Java/Don't know why I'm getting a null pointer exception
Expert: Brian Ngure - 11/28/2006
QuestionHi,
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);
}
}
AnswerHi,
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);
}
}