Sunday, 15 April 2012

java - Null pointer exception when changing int in another class -



java - Null pointer exception when changing int in another class -

ive looked , looked , cant find answer. here issue. wanting alter int in class based on ticks in main game class. here code:

trace:

02-16 02:10:58.605: w/dalvikvm(1578): threadid=13: thread exiting uncaught exception (group=0x40c5ca68) 02-16 02:10:58.615: e/androidruntime(1578): fatal exception: thread-16720 02-16 02:10:58.615: e/androidruntime(1578): java.lang.nullpointerexception 02-16 02:10:58.615: e/androidruntime(1578): @ com.mojang.ld22.game.tick(game.java:374) 02-16 02:10:58.615: e/androidruntime(1578): @ com.mojang.ld22.game.iterate(game.java:300) 02-16 02:10:58.615: e/androidruntime(1578): @ com.mojang.ld22.gameactivity$4.run(gameactivity.java:136) 02-16 02:10:58.615: e/androidruntime(1578): @ java.lang.thread.run(thread.java:856)

game.java snippet

public void tick() { tickcount++; ... ... ... if (tickcount % 1000 == 0) { if (rising) { lightlvl++; error here clock.time++; <----------- if (lightlvl >= 8) { rising = false; } } else { lightlvl--; line 374 ----------> clock.time--; <------------ if (lightlvl <= -2) rising = true; } } } }

and clock.java

bundle com.mojang.ld22.entity; import com.mojang.ld22.crafting.crafting; import com.mojang.ld22.entity.particle.textparticle; import com.mojang.ld22.entity.particle.timeclock; import com.mojang.ld22.gfx.color; import com.mojang.ld22.gfx.screen; import com.mojang.ld22.screen.craftingmenu; import com.mojang.ld22.item.furnitureitem; public class clock extends piece of furniture { /** * */ private static final long serialversionuid = 3146189783597003582l; player player; public int time = 0; public int clock = 1; public boolean rising = false; public clock() { super("clock"); col = color.get(-1, 100, 321, 431); sprite = 10; xr = 3; yr = 2; } @override public void tick(){ if (time <= 0) clock = 1; if (time == 1) clock = 1; if (time == 2) clock = 2; if (time == 3) clock = 3; if (time == 4) clock = 4; if (time == 5) clock = 5; if (time == 6) clock = 6; if (time == 7) clock = 7; if (time == 8); clock = 8; } @override public void render(screen screen){ int col = color.get(-1, 531, 000, 534); //incorrect times todo if (clock == 8) screen.render(x - 8, y - 8, 7 + 11 * 32, col, 0); if (clock == 6) screen.render(x - 8, y - 8, 8 + 11 * 32, col, 0); //noon midnight if (clock == 5) screen.render(x - 8, y - 8, 9 + 11 * 32, col, 0); if (clock == 4) screen.render(x - 8, y - 8, 10 + 11 * 32, col, 0); //before noon after noon if (clock == 3) screen.render(x - 8, y - 8, 11 + 11 * 32, col, 0); if (clock == 2) screen.render(x - 8, y - 8, 12 + 11 * 32, col, 0); //730pm 530pm if (clock == 1) screen.render(x - 8, y - 8, 13 + 11 * 32, col, 0); if (clock == -1) screen.render(x - 8, y - 8, 14 + 11 * 32, col, 0); } }

and yes isn't pretty. tried when couldn't pull int game.java clock.java test sending it. cant utilize separate ticks because clock.java isn't created until player pulls his/her inventory.

i hope makes since. can supply more code if need it.

rising false, else branch executed , clock null, throws nullpointerexception.

by way, write tick method this:

public void tick(){ if (time <= 0) clock = 1; else if (time <= 8) clock = time; }

java android nullpointerexception int

No comments:

Post a Comment