Java using an object from a central class -


i making game in java , have central(engine) class render scenery/player/etc.

in engine class create player object so

public class engine() {      public static player player;      public engine() {         renderplayer();     }      protected static void renderplayer() {         player = new player();     } } 

i have canvas class handles drawing of player. want able call players functions without having create new instance( since created player in engine class ). whenever try use function player, null pointer expection.

this how call it.

engine.player.tick(); 

i have spent couple hours trying figure out, may tell me doing wrong, , me in right direction? thank you.

you need call engine.renderplayer() prior calling engine.player.tick(); method assigns new player instance static player attribute. before call method player null , causing nullpointerexception.

you can fix issue changing:

public static player player; 

to:

public static player player = new player(); 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -