Help with error on second run through array
I'm having some issues with entering data into my array. The first run through the loop has no issues...that I can see, but once I have entered the first batch of information I start getting Null Pointer Exception errors. Here is the exact error:
"Exception in thread "main" java.lang.NullPointerException at Employee.main(Employee.java:108)"
On the second run through the loop, if I try to add a second set of data, the NPE is on line 108. If I try to view the data it's on line 112. I can't figure out what variable is being considered null. Can anyone help?
Code :
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final int size = 100; //max array size
int numEmployee=0; //number of array entries
int i;
int choice=0;
Employee [] employeeData = new Employee[size]; //an array of objects
employeeData[numEmployee] = new Employee();
System.out.println("1. Enter new employee data");
System.out.println("2. Display employee data");
System.out.print("3. Quit \n=> ");
choice = input.nextInt();
while(choice!=3){
if(choice == 1){
employeeData[numEmployee].setFirstName(); //******line 108 NPE is here******
numEmployee++;
}else if(choice == 2){
for(i = 0; i<numEmployee; i++);
System.out.println(employeeData[i].getFirstName()); //******line 112 NPE is here******
}else{
System.out.println("Invalid entry. Please try again.");
} //close if
System.out.println("1. Enter new employee data");
System.out.println("2. Display employee data");
System.out.print("3. Quit \n=> ");
choice = input.nextInt();
} //close while
System.out.println("You have quit.");
} //close main
Re: Help with error on second run through array
What variable is null at line 108 where the NPE happens? Why doesn't that variable have a valid non-null value?
There are two steps when using an array of objects:
The first step creates the array with empty slots.
The second step puts an object into the slots.
Are you sure your code has put an object into all the slots in the array that it is trying to access? If you haven't assigned an object to a slot, its contents will be null and you'll get a NPE when you try to use it.
Re: Help with error on second run through array
That's my problem. I don't know what is null at line 108. Nothing is null the first time I run through, so I don't understand why something is null the second time. Is there a way to find out what is being considered null?
Re: Help with error on second run through array
Quote:
don't know what is null at line 108
Add a println statement to print out the values of all the variables used on line 108 to see which one is null.
Did you understand my last post about using arrays of objects? Where do you put an object into the array at the slot you are trying to use?
Re: Help with error on second run through array
If I'm not mistaken, I'm putting objects into my array with line 108...am I not? But if that's the case, I am getting the NPE because the entry is not being put into the array properly. Is that what you meant?
And according to my println, employeeData[numEmployee] is still null even after I've entered data. Though, numEmployee has been given a value of 1 like it was supposed to.
Re: Help with error on second run through array
To see what is in the array at line 108, add a call to the println method just before line 108 and print out the contents of the element in the array the code is trying to access.
The only place I see where you are putting objects into the array is this line:
Code :
employeeData[numEmployee] = new Employee();
Re: Help with error on second run through array
Sorry, not sure I understand what you mean. How do I add a call to the println?
Re: Help with error on second run through array
Quote:
How do I add a call to the println?
Here's a sample call to the println method from your program:
Code :
System.out.println("2. Display employee data");
Put what you want to see printed inside of the ()s
Re: Help with error on second run through array
Ah sorry, misunderstood what you wrote. That I know how to do.
so, to call the elements in the array I should write:
System.out.println(employeeData[numEmployee]); ?
But all I get is null as a result.
Sorry, this is my first time working with arrays, so it's all still pretty confusing to me.
Re: Help with error on second run through array
Quote:
But all I get is null as a result.
That shows that there is no object in the array at that slot. See my post #2
You can print out the whole contents of an array with:
Code :
System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
Replace theArrayName with the name of your array.
Make sure that you have put an object into the array before you try to call the object at that slot.
Re: Help with error on second run through array
Wow, I'm having a hard time accessing the forums right now. Anyway...
Okay, tried that and I got 'Employee@3c939d40' for the first entry and then I got null values. I'm assuming 99 null values since my array is 100 units in size. Though 'Employee@3c939d40' isn't the content I entered into the array.
So, I need to do something about all the extra null values in order to solve my problem?
Re: Help with error on second run through array
That looks about right for the code you posted. The array has 100 slots and the code has added one Employee object to the first slot. The rest of the 99 slots are empty and have null values.
You need to add Employee objects to more of the slots as needed.
'Employee@3c939d40' is the String returned by the Object class's toString() method. Its the classname, a @ and a unique hex number. If you want to see something different, override the Employee class's toString() method and return the String you want to see.
Re: Help with error on second run through array
Okay this is slowly starting to make sense, but how do I fix my NPE problem without putting data into all 100 slots? 100 is just the max number of slots the user has to work with, but they don't have to use them all.
Re: Help with error on second run through array
You need to add a new object every time the user requests you to add one when he enters a "1".
Re: Help with error on second run through array
So, if I move my employeeData[numEmployee] = new Employee(); object down into the loop, that should create a new object each time the user chooses 1.
Something like this:
employeeData[numEmployee] = new Employee();
employeeData[numEmployee].setFirstName();
numEmployee++;
Re: Help with error on second run through array
That looks about right. How does the code do now when compiled and executed.
Re: Help with error on second run through array
It's almost there. The NPE error at line 108 is gone. I can enter as much info as I want. Now I've just got to deal with the NPE error at line 112 where the user chooses to view the data that has been entered.
Thank you so much for all this help. I really appreciate it.
Re: Help with error on second run through array
Good luck finding the problem. Using the above discussed techniques should help.
Re: Help with error on second run through array
Alright, thanks again. I should be able to figure it out at this point.
--- Update ---
I solved it! Who knew a ; could cause so much chaos, haha. I'll definitely remember all these troubleshooting tips you gave me :)