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);
}
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.