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.
Printable View
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.
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:
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:Code:myFloors[s].unloadPassengers(s);
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);
}
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:
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.Code:static Floor[] myFloors = new Floor[FLOORS];
Thanks that worked.
Cool, glad it helped.