For getting the correct level in the game I'm working on, I want to have and integer "level" and add one to it when the level is completed. Then in my "Levels" class, I have it set it to the appropriate level. This isn't working though. Here's some code to explain -

Check the original post; I updated with the full source and a batch+jar so you can see the error in command prompt.

Just pasting the stack trace here would probably be sufficient. Again, what line is the NPE being thrown from? If it's really one like you show in your post, then it can only be "l" that is null. If it's actually complaining about one of the two lines *inside* getMap(), then it's got to be "p" that is null.

NPE's are fun and easy to fix, because the stack trace points right to the problem. Just see which variable you're de-referencing. It must be null. If you're de-referencing more than one, it's still usually easy to figger out which one is the culprit. And if it isn't, well, that's what debuggers are for.

Might be easier for you to download the zip I posted on the first post.

But here it is -

Exception in thread "Thread-3" java.lang.NullPointerException at main.Player.checkCollision(Player.java:114) at main.Player.update(Player.java:180) at main.Main.logicUpdates(Main.java:167) at main.Main.tick(Main.java:159) at main.Main.run(Main.java:130) at java.lang.Thread.run(Unknown Source)

Might be easier for you to download the zip I posted on the first post.

But here it is -

Exception in thread "Thread-3" java.lang.NullPointerException at main.Player.checkCollision(Player.java:114) at main.Player.update(Player.java:180) at main.Main.logicUpdates(Main.java:167) at main.Main.tick(Main.java:159) at main.Main.run(Main.java:130) at java.lang.Thread.run(Unknown Source)

Actually, I'm lazy, I'd rather just you give me the stack trace then download and run something. My point was just to read what the stack trace is telling you. NPE's are easy to fix. What's line 114 of Player.java? If it's indeed "l.getMap(level)", then l must be null. Just trace back to see why l isn't set.

@OPCheck line 114 on checkCollision method, what ever object references you call methods on at that line are null

I did. That's exactly what my previous reply told him. I've also confirmed that line 114 is indeed the "l.getMap(level)" line.

@OP: As I said, NPE's are literally the easiest bugs you'll ever fix. You just need to work backwards through your logic until you find something being null that shouldn't be...

Look at line 114 of Player.java. Note that it's just

1

l.getMap(level);

Here, "l" is the only object you're de-referencing, so it must be null. So think, where is "l" getting set in the Player class? It appears to be done in the constructor. So go through your code and find all places where you instantiate a new Player. See what you're passing in for the third argument. Then you should be able to spot your problem.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org