Results 1 to 5 of 5
  1. #1
    soxfan714 is offline Member
    Join Date
    Nov 2008
    Posts
    17
    Rep Power
    0

    Default [SOLVED] Help with understanding nullpointexcepion

    I am new to java and I am having some problems with nullpointexception. This is the first time I have encountered this problem so I would love any help in resolving it/helping me understand why I am getting this error would be appreciated.
    Last edited by soxfan714; 11-11-2008 at 05:01 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    A NPE is telling you that you are trying to "dereference" or use an object that has not yet been initialized, that is set equal to null. To figure out which object this is, you need to look at the line that's throwing the error and then test each object there to see what is set to null. The line that I see causing this is:
    Java Code:
        myFloors[s].unloadPassengers(s);
    To find out which object is at fault, use a poor-man's debugger, the System.out.println statement. I changed your code to read:
    Java Code:
      private static void stop(int s)
      {
        System.out.println("\nStoping on floor " + currentFloor);
        System.out.println("myFloors == null: " + (myFloors == null));
        System.out.println("myFloors[s] == null: " + (myFloors[s] == null));
    
        myFloors[s].unloadPassengers(s);
      }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Now you have to figure out why this error is being thrown. So if you look at the myFloors variable, you'll see that it has been initialized properly here:
    Java Code:
    static Floor[] myFloors = new Floor[FLOORS];
    but where are the myFloors items that are held by the array being initialized? In other words if there are 4 items in the array, where are myFloors[0], myFloors[1], myFloors[2], and myFloors[3] being initialized? No where.

  4. #4
    soxfan714 is offline Member
    Join Date
    Nov 2008
    Posts
    17
    Rep Power
    0

    Default

    Thanks that worked.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 3
    Last Post: 10-07-2008, 12:03 AM
  2. Understanding Vectors
    By cbrown08 in forum New To Java
    Replies: 7
    Last Post: 11-05-2007, 07:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •