0

I am getting this error whenever I press spacebar(to shoot the 'missile'), and I can't find out how to fix it, at all.

Exception in thread "Thread-0" java.lang.NullPointerException
    at com.game.main.Bullet.render(Bullet.java:26)
    at com.game.main.Controller.render(Controller.java:58)
    at com.game.main.Game.render(Game.java:155)
    at com.game.main.Game.run(Game.java:108)
    at java.lang.Thread.run(Thread.java:745)

I will list the code that is in the error as I have 9 classes.

The Bullet error line:

g.drawImage(tex.missile, (int)x, (int)y, null);

tex.missile is a BufferedImage variable from the texture class, and is set to

missile = ss.grabImage(2, 1, 32, 32);

ss is a SpriteSheet class and is set to new SpriteSheet(game.getSpriteSheet());

The SpriteSheet class just gets the SpriteSheet and just sets each square to a row/column etc. Everything there works as my Player & Enemy both work.

Controller error line:

tempBullet.render(g);

Game.render error line:

c.render(g);

Game.run error line:

render();

I highly doubt it has anything to do with the Game class, or anything like that, but I think it has to do with the Bullet class, but yet again. I could be, and most likely am wrong. I think that something may not be set correctly.

If you want me to show a whole class, I will. Just tell me which one you need.

EDIT: g.drawImage is inside of this method

public void render(Graphics g){ 
    g.drawImage(tex.missile, (int)x, (int)y, null);
}
b4hand
  • 9,550
  • 4
  • 44
  • 49
BiblyDoo
  • 25
  • 1
  • 7
  • What type is `g` when you call `g.drawImage(...)`? – Foggzie Nov 01 '14 at 23:18
  • 2
    Test if one of these is null: `g`, `tex`, `x` or `y` (x and y can be null if you use wrapper classes like `Integer`). – Tom Nov 01 '14 at 23:19
  • @GuntherFox Most likely a `Graphics` instance: http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#drawImage%28java.awt.Image,%20int,%20int,%20java.awt.image.ImageObserver%29 – Tom Nov 01 '14 at 23:19
  • 3
    You need to show the cause of the error. More importantly, you need to learn the general concepts of how to debug a NPE (NullPointerException). **You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully**, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. So find out what's null and let us know. My guess -- you're using a `getGraphics()` call on a component to get your Graphics object, and Graphics, g, is null – Hovercraft Full Of Eels Nov 01 '14 at 23:21
  • @Tom I will do now, I'll get back to you when I've checked :) – BiblyDoo Nov 01 '14 at 23:21
  • Please show enough code so that we can understand your problem. – Hovercraft Full Of Eels Nov 01 '14 at 23:25
  • @Tom Thanks, I checked and tex was null, in my constructor, I just had to define tex, so I did this.tex = tex; and it works now, thankyou! – BiblyDoo Nov 01 '14 at 23:27
  • If anyone wants to put an answer then I'd be happy to tick it :) – BiblyDoo Nov 01 '14 at 23:28
  • @HovercraftFullOfEels Thankyou too! Helped me find out which Variable is null, helped alot! – BiblyDoo Nov 01 '14 at 23:29
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – xenteros Sep 28 '16 at 11:51

2 Answers2

2

A NullPointerException occurs if you're trying to perform an action (like a method call) on a null reference. So if you're getting this exception on this line g.drawImage(tex.missile, (int)x, (int)y, null);, then check if one of the following variables is null:

  • g
  • tex
  • x
  • y

x and y can be null, if you use a wrapper class like Integer. The provided null reference for the image observer (forth argument of the method drawImage) won't cause a NullPointerException, so there is no need to change something there.

Tom
  • 16,842
  • 17
  • 45
  • 54
  • http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – xenteros Sep 28 '16 at 11:51
  • @xenteros I know (now; this answer is 2 years old), but this answer has been accepted and I can't delete it anymore. – Tom Sep 28 '16 at 12:14
0

To give a precie answer, a wider description would be necessary, i.e. what framework you are using etc.. From what you write I assume the following:

  • You're writing in Java.
  • You're working on a game.
  • You're using some renderer to render something.

Did you write that renderer yourself? Are you using a framework? What platform are you actually targeting?

This might already help you: I'd guess that "g" hasn't been initialized or is just set to null. Try setting a breakpoint before the line where your code crashes, run the debugger and see what "g" contains. If it does not point to null, the problem might be somewhere else. Also note that you're passing "null" as the last parameter, which might not be allowed here. ;)

OrangeCMS
  • 55
  • 4