2

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)
9
  • 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. Commented Feb 24, 2010 at 7:12
  • this spans across several different files and would be extremely difficult to do so as it is over 5000 lines of code. However, I think this is an easy fix for experienced users and should take less than 5 mins. Commented Feb 24, 2010 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? Commented Feb 24, 2010 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. Commented Feb 24, 2010 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. Commented Feb 24, 2010 at 7:18

1 Answer 1

9

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.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks you for your help. But what do you mean by "forgotten to construct the collection."
@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.
WOW! That actually did it! Thanks!! I would email you a $10 if it were possible...
@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. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.