views:

272

answers:

1

This may be a little unconventional way of asking for help but my code is running into null pointer runtime errors but the scope of the runtime error is too big to post onto stackoverflow. I really want to figure this out so would it be possible for me to email one of you my code to figure out what is wrong? I know runtime errors tell the specific line number it's tripping on but I honestly can't make heads or tails why it's happening there. Thank you very much!!

Stack trace:

java.lang.NullPointerException
    at Maze.getNumRandOccupants(Maze.java:118)
    at P4TestDriver.testMaze(P4TestDriver.java:995)
    at P4TestDriver.main(P4TestDriver.java:116)
    at __SHELL8.run(__SHELL8.java:7)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at bluej.runtime.ExecServer$3.run(ExecServer.java:814)
java.lang.NullPointerException
    at Maze.addRandomOccupant(Maze.java:130)
    at P4TestDriver.testMazeReadWrite(P4TestDriver.java:1071)
    at P4TestDriver.main(P4TestDriver.java:127)
    at __SHELL8.run(__SHELL8.java:7)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at bluej.runtime.ExecServer$3.run(ExecServer.java:814)
+7  A: 

From your comment:

public int getNumRandOccupants() { return randOccupants.size(); }

Because this is at the top of your stack trace, it means that the randOccupants field is null at the time this method is called.

Also, if you are getting another NPE at addRandomOccupant, the same collection is probably null there, too. You likely have simply forgotten to construct the collection.

Mike Daniels
thanks you for your help. But what do you mean by "forgotten to construct the collection."
Kevin Duke
@Kevin: I assume the `Maze` class has a field representing a `Collection` of occupants. Something like `private List<Occupant> randOccupants;`. I think you have declared the field, but forgotten to initialize it to an instance of the proper collection, i.e. `private List<Occupant> randOccupants = new ArrayList<Occupant>();`. You could also construct the collection in the constructor for `Maze`.
Mike Daniels
WOW! That actually did it! Thanks!! I would email you a $10 if it were possible...
Kevin Duke
@Kevin: no problem. We all miss little things like this from time to time. The only reason I was able to zero in on the answer is because I constantly make this same error, after years of writing Java. ;)
Mike Daniels