Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Help with error on second run through array

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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?

    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with error on second run through array

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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:
     employeeData[numEmployee] = new Employee();
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with error on second run through array

    How do I add a call to the println?
    Here's a sample call to the println method from your program:
    System.out.println("2. Display employee data");

    Put what you want to see printed inside of the ()s
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with error on second run through array

    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:
    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.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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".
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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++;

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with error on second run through array

    That looks about right. How does the code do now when compiled and executed.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with error on second run through array

    Good luck finding the problem. Using the above discussed techniques should help.
    If you don't understand my answer, don't ignore it, ask a question.

  19. The Following User Says Thank You to Norm For This Useful Post:

    tyneframe (November 25th, 2012)

  20. #19
    Junior Member
    Join Date
    Sep 2012
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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

Similar Threads

  1. run time error
    By nayeemohamed in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 6th, 2012, 04:33 PM
  2. Run Time Error
    By Tanmaysinha in forum What's Wrong With My Code?
    Replies: 22
    Last Post: March 8th, 2012, 11:29 AM
  3. Help with run time error
    By white97 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: August 11th, 2011, 12:35 PM
  4. Run Time Error.
    By seymour001 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 10th, 2011, 12:10 PM
  5. [SOLVED] Run time error
    By moodycrab3 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2011, 11:05 AM