AllExperts > Java 
Search      
Java
Volunteer
Answers to thousands of questions
 Home · More Java Questions · Answer Library  · Encyclopedia ·
More Java Answers
Question Library

Ask a question about Java
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Brian Ngure
Expertise
Strong in Java. Will be able to answer both in areas of designing and coding. Some knowledge in servlets

Experience
5 years of Java experience, Sun Certified Java Associate and Sun Certified Java Programmer, JDK 1.4

 
   

You are here:  Experts > Computing/Technology > Focus on Java > Java > Don't know why I'm getting a null pointer exception

Java - Don't know why I'm getting a null pointer exception


Expert: Brian Ngure - 11/28/2006

Question
Hi,
I'm teaching myself Java and I'm trying to use an array of objects I created but I'm getting a null pointer exception the first time I try to assign one of the fields. When I instantiate an array of objects doesn't that instantiate each object of the array? The code is below and the line I get the error on is indicated. Thanks!

//This program calculates the gross pay for three employees
import javax.swing.*;

public class TestDriver {
  //Employee class
  public class Employee{
     double hours;
     double wage;
     double totalPay;
  }
  public static void main(String[] args){
     //Array of employees
     Employee employeeArray[] = new Employee[3];
     //Temp string
     String temp;
     
     //Get and process employee data
     for( int i = 0; i < 3; i++ ){

        temp = JOptionPane.showInputDialog("Enter wage for employee " + (i+1));
        employeeArray[i].wage = Double.parseDouble(temp);  //I get a null pointer exception here
        
        temp = JOptionPane.showInputDialog("Enter hours for employee " + (i+1));
        employeeArray[i].hours = Double.parseDouble(temp);
        
        if( employeeArray[i].hours <= 40.00){
           employeeArray[i].totalPay = employeeArray[i].wage * employeeArray[i].hours;
        }
        else{
           employeeArray[i].totalPay =
              (employeeArray[i].wage * 40) + (1.5 * employeeArray[i].wage *
                    (employeeArray[i].hours - 40));
        }
     }
     temp = "Employee pay results:\n";
     //Create output string
     for( int i = 0; i < 3; i++ ){
        temp += "Employee " + (i+1) + " worked " + employeeArray[i].hours +
        "\nat $" + employeeArray[i].wage + " per hour for a gross pay of $" +
        employeeArray[i].totalPay + ".\n";
     }
     
     //Display output
     JOptionPane.showMessageDialog(null, temp);
     
     System.exit(0);
        
  }
}

Answer
Hi,

When I instantiate an array of objects doesn't that instantiate each object of the array? -> No it does not. The three elements in the array are still null.

See the comments I have added to your modified code below for more details.



import javax.swing.JOptionPane;

public class TestDriver {
   //Employee class
   public class Employee {
       double hours;
       double wage;
       double totalPay;
   }


   public static void main(String[] args) {
       // Create an instance of this class to reference the Employee class
       TestDriver td = new TestDriver();
       //Array of employees
       Employee[] employeeArray = new Employee[3];
       
       // Initially, each object in the array is null (not auto-initialized)
       // Use td reference to create a new Employee object
       employeeArray[0] = td.new Employee();
       employeeArray[1] = td.new Employee();
       employeeArray[2] = td.new Employee();
       
       // Proceed with your unchanged code
       
       //Temp string
       String temp;

       //Get and process employee data
       for (int i = 0; i < 3; i++) {
           temp = JOptionPane.showInputDialog("Enter wage for employee " + (i + 1));
           employeeArray[i].wage = Double.parseDouble(temp); // null pointer exception does not occur

           temp = JOptionPane.showInputDialog("Enter hours for employee " + (i + 1));
           employeeArray[i].hours = Double.parseDouble(temp);

           if(employeeArray[i].hours <= 40.00) {
               employeeArray[i].totalPay = employeeArray[i].wage * employeeArray[i].hours;
           } else {
               employeeArray[i].totalPay = (employeeArray[i].wage * 40) + (1.5 * employeeArray[i].wage * (employeeArray[i].hours - 40));
           }
       }
       temp = "Employee pay results:\n";
       //Create output string
       for (int i = 0; i < 3; i++) {
           temp += "Employee " + (i + 1) + " worked " + employeeArray[i].hours + "\nat $" + employeeArray[i].wage + " per hour for a gross pay of $" + employeeArray[i].totalPay + ".\n";
       }

       //Display output
       JOptionPane.showMessageDialog(null, temp);

       System.exit(0);
   }
}


Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.