eclipse - Why is there a java.lang.NullPointerException when referencing Image from a different class? -
i creating clone of breakout pc, earlier tonight found there nullpointerexception being thrown , shown in console. oddly, method run()
told catch exceptions occour , show stacktrace , ignore them, time freezes game @ start, int log, keypresses still tracked, nothing happens in-game. obviously, game didn't abort, not repaint.
it seems coming 1 line of code:
if (by >= bricks[0].y && <= bricks[0].y + bricks[0].img.getheight(null) && bx >= bricks[0].x && bx <= bricks[0].x + bricks[0].img.getwidth(null)) {
here log , stacktrace (the entire thing):
game starting, menu boot. menu loaded, jframe visible. running thread. play clicked, start game initialization. creating jframe. jframe created , visible. starting game thread initialize user variables. initialized user variables. initialization complete, start game. java.lang.execption in line 86/87, class game. stacktrace: java.lang.nullpointerexception @ game.move(game.java:195) @ game.run(game.java:86) @ java.lang.thread.run(thread.java:722) space hit! key arrowleft hit. key arrowleft hit. key arrowleft hit. key arrowleft hit. key arrowleft hit. key arrowleft hit. key arrowleft hit. key arrowleft released. key escape hit!
i hope problem can solved.
lets @ "line":
if (by >= bricks[0].y && <= bricks[0].y + bricks[0].img.getheight(null) && bx >= bricks[0].x && bx <= bricks[0].x + bricks[0].img.getwidth(null)) {
assuming bx
, by
, , x
, y
fields primitive types (e.g. int
), here's list of things could result in npe:
- the
bricks
variablenull
- the zeroth element of
bricks
null
- the value of
bricks[0].img
null
if bx
, by
, x
and/or y
wrapper type, 1 of null
. lead npe when unboxing value.
it not caused passing null
getheight
or getwidth
methods, because 1) believe valid , 2) if wasn't npe thrown in different place stacktrace shows.
(i can't tell if passing null
in calls correct ... either way, not cause of npe based on evidence have provided.)
if want figure actually causing problem, debug code or add traceprints test of things null
null
.
Comments
Post a Comment