Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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)
share|improve this question
1  
Just post the code. By posting your problem publicly, and having the solutions right below it, you might be able to provide insight for others in the future. –  Mike Daniels Feb 24 '10 at 7:12
2  
and the stacktrace too –  flybywire Feb 24 '10 at 7:14
1  
post the minimal scenario that reproduces your issue. Start cutting the tree, you don't claim that each and every of the 5000 lines play a role in causing the exception, do you? –  flybywire Feb 24 '10 at 7:16
1  
@Kevin: The stack trace will, however, isolate the specific class and line that the NPE occurred at; just that method and perhaps the calling method is likely all we need. –  Lawrence Dol Feb 24 '10 at 7:16
1  
Is it even possible to send private messages on Stack Overflow? I don't think you're going to convince anyone to post their e-mail address permanently for spambot crawlers to find. –  Mike Daniels Feb 24 '10 at 7:18

1 Answer 1

up vote 7 down vote accepted

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.

share|improve this answer
    
thanks you for your help. But what do you mean by "forgotten to construct the collection." –  Kevin Duke Feb 24 '10 at 7:27
1  
@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 Feb 24 '10 at 7:30
    
WOW! That actually did it! Thanks!! I would email you a $10 if it were possible... –  Kevin Duke Feb 24 '10 at 7:39
1  
@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 Feb 24 '10 at 7:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.