0

I've got a function that grabs the players X co-ords and then returns them, but its returning null for some reason I can't quite figure out. (Hence why I'm here).

The exact error I get is as follows:

java.lang.NullPointerException
at dev.colors.level.Level.getXOffset(Level.java:78)

All i do is call this, this is line 78:

if(player.getX() <= half_width){

This line come from this method:

public int getXOffset(){
    int offset_x = 0;
    //the first thing we are going to need is the half-width of the screen, to calculate if the player is in the middle of our screen
    int half_width = (int) (Game.WINDOW_WIDTH/Game.SCALE/2);

    //next up is the maximum offset, this is the most right side of the map, minus half of the screen offcourse
    int maxX = (int) (map.getWidth()*32)-half_width;

    //now we have 3 cases here
    if(player.getX() <= half_width){
        //the player is between the most left side of the map, which is zero and half a screen size which is 0+half_screen
        offset_x = 0;
    }else if(player.getX() > maxX){
        //the player is between the maximum point of scrolling and the maximum width of the map
        //the reason why we substract half the screen again is because we need to set our offset to the topleft position of our screen
        offset_x = maxX-half_width;
    }else{
        //the player is in between the 2 spots, so we set the offset to the player, minus the half-width of the screen
        offset_x = (int) (player.getX()-half_width);
    }

    return offset_x;
}

The getX method is 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);
    return x;
  }
}

We declare LevelObject by creating a Player object:

player = new Player(128, 64);

Which then hands the two variables delcared there through the Player.java class, and then through the 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 properly up until we call getX (in the LevelObject class, even when I use the System.out to print "this.x" from LevelObject before we call getX it returns the correct variable, "128.0" as declared by the Player object.

To make things weirder, if i put some printlines inside the getX method, they don't show up in the console. It's as if it doesn't even run the method.

This doesn't make any sense to me, and I'm very very lost.

  • 1
    Would you please post a reproductible example ? I highly doubt this is a paste of the real example as this would not compile. – Jean-François Savard Apr 12 '15 at 20:42
  • 2
    I highly doubt that the return value of `getX()` is `null` since it returns a primitive and primitives cannot be `null`. Please be more specific. – Turing85 Apr 12 '15 at 20:43
  • Needs more context, how is the object created etc. Also you said it goes null. Do you mean you get a null pointer exception on player? X itself is a primitive type and can't be null. – JHH Apr 12 '15 at 20:45
  • Well the full example would require my whole code, its a long list of stuff all leading up to it, it has to do with creating the player object, then setting the x as the current players position, i can throw the repo I'm using at you though. – Alexander Smith-Richard Apr 12 '15 at 20:48
  • If you want us to help you then boil it down to the exact location the problem occurs. Don't throw unnecessary source code at us. – Turing85 Apr 12 '15 at 20:50
  • Ok, I've updated it, if that's not enough i can attempt to add more. – Alexander Smith-Richard Apr 12 '15 at 21:04

1 Answers1

0

Apparently, the player variable is null. The getXOffset() does not initialize a player variable, so I guess it must be a class field or something, which must be initialized in a different method. Perhaps you need to add the this keyword to player initialization code (make it look like this.player = new Player(128, 64);) ?

Either way, your player variable is not initialized properly and that is the reason for that exception.

  • On second look you were half right, you were correct that the player variable was not declared, but it was because i had two things mixed up: I rendered the map with a player before i ever declared what a player was! – Alexander Smith-Richard Apr 12 '15 at 21:33