java - player.getX() returning null -
java - player.getX() returning null -
i've got function grabs players x co-ords , returns them, returning null reason can't quite figure out. (hence why i'm here).
the exact error follows:
java.lang.nullpointerexception @ dev.colors.level.level.getxoffset(level.java:78)
all phone call this, line 78:
if(player.getx() <= half_width){
this line come method:
public int getxoffset(){ int offset_x = 0; //the first thing going need half-width of screen, calculate if player in middle of our screen int half_width = (int) (game.window_width/game.scale/2); //next maximum offset, right side of map, minus half of screen offcourse int maxx = (int) (map.getwidth()*32)-half_width; //now have 3 cases here if(player.getx() <= half_width){ //the player between left side of map, 0 , half screen size 0+half_screen offset_x = 0; }else if(player.getx() > maxx){ //the player between maximum point of scrolling , maximum width of map //the reason why substract half screen 1 time again because need set our offset topleft position of our screen offset_x = maxx-half_width; }else{ //the player in between 2 spots, set offset player, minus half-width of screen offset_x = (int) (player.getx()-half_width); } homecoming offset_x; }
the getx method here:
public abstract class levelobject { protected float x; public levelobject(float x, float y) { system.out.println(x); this.x = x; this.y = y; public float getx() { system.out.println(this.x); homecoming x; } }
we declare levelobject creating player object:
player = new player(128, 64);
which hands 2 variables delcared there through player.java class, , through character.java class:
public class player extends character { public player(float x, float y) throws slickexception { super(x, y); }
character.java:
public abstract class character extends levelobject { public character(float x, float y) throws slickexception { super(x, y); }
}
everything goes through until phone call getx (in levelobject class, when utilize system.out print "this.x" levelobject before phone call getx returns right variable, "128.0" declared player object.
to create things weirder, if set printlines within getx method, don't show in console. it's if doesn't run method.
this doesn't create sense me, , i'm very lost.
apparently, player
variable null. getxoffset()
not initialize player
variable, guess must class field or something, must initialized in different method. perhaps need add together this
keyword player initialization code (make this.player = new player(128, 64);
) ?
either way, player
variable not initialized , reason exception.
java variables methods null getter
Comments
Post a Comment