Sunday, 15 June 2014

NullPointerException on overlap in libgdx (Java) -



NullPointerException on overlap in libgdx (Java) -

i'm making simple java game part of me learing , when run a:

exception in thread "lwjgl application" java.lang.nullpointerexception @ com.mattihase.floatingpoint.gamescreen.generalupdate(gamescreen.java:65) @ com.mattihase.floatingpoint.gamescreen.render(gamescreen.java:42) @ com.badlogic.gdx.game.render(game.java:46) @ com.badlogic.gdx.backends.lwjgl.lwjglapplication.mainloop(lwjglapplication.java:207) @ com.badlogic.gdx.backends.lwjgl.lwjglapplication$1.run(lwjglapplication.java:114)

this general update line it's talking , lines under it:

if(pc.bounds.overlaps(gary.rbounds)) //where test bunch of booleans see dialog returned should //yes, know i'm doing ifs unoptimized couldn't //work other way. //the pc.bounds stuff there create follow player character around //world photographic camera this. { if(pc.onquest = false){ if(pc.finishquest = false){ assets.font.draw(batch, gary.gdio, pc.bounds.x - 250, pc.bounds.y - 190); pc.onquest = true; } } if(pc.onquest = true){ if(pc.finishquest = false){ assets.font.draw(batch, gary.gddio, pc.bounds.x - 250, pc.bounds.y - 190); } } if(pc.haspoints = true){ pc.finishquest = true; } if(pc.finishquest = true); assets.font.draw(batch, gary.gcdio, pc.bounds.x - 250, pc.bounds.y - 190); pc.onquest = false; pc.award = true; }`

and these classes playercharacter , npc (called pc (class playercharacter) , gary (class rectoid) respectively) player character

public sprite image; public rectangle bounds; public int score; public string scoreprint; public boolean onquest; public boolean finishquest; public boolean haspoints; public boolean award; public playercharacter(){ onquest = false; haspoints = false; finishquest = false; award = false; score = 0; image = assets.s_pc; bounds = new rectangle(256-16, 192-16, 16, 16); scoreprint = score + "";`

and gary `public sprite rimage; public rectangle rbounds; public string gdio; public string gddio; public string gcdio;

public rectoid() { rimage = assets.s_rectoid; rbounds = new rectangle(1024, 64, 64, 32); gdio = "the diolog"; gddio = "the diolog"; gcdio = "the diolog";`

c

in java, check equality in objects references or primitive types have utilize == operator. in lines if(pc.onquest = false){ assigning false onquest, instead of checking value. reason why code compiles because in java assignment returns assigned value. illustration sentence:

int 0 = 0; if (zero == 0){} //its true if (zero = 0){} //compile error, becouse 'zero = 0' returns '0', asigned value

what happening in code using boolean variables.

if(pc.bounds.overlaps(gary.rbounds)) { if(pc.onquest = false){ // here doing 2 things: assing false // onquest , homecoming false. next code never execute if(pc.finishquest = false){ // same here assets.font.draw(batch, gary.gdio, pc.bounds.x - 250, pc.bounds.y - 190); pc.onquest = true; } } if(pc.onquest = true){// same here if(pc.finishquest = false){// same here assets.font.draw(batch, gary.gddio, pc.bounds.x - 250, pc.bounds.y - 190); } } if(pc.haspoints = true){// same here pc.finishquest = true; } if(pc.finishquest = true);// same here assets.font.draw(batch, gary.gcdio, pc.bounds.x - 250, pc.bounds.y - 190); pc.onquest = false; pc.award = true; }`

also, check value of boolean utilize it, no compare true or false, , create utilize of ! operator.

just simple utilize if(!pc.onquest){ if want check if onquest disabled.

about nullpointereception, gary variable null. check if has create before. should like:

rectoid gary = new rectoid();

java nullpointerexception libgdx

No comments:

Post a Comment