简体   繁体   中英

How to reference object parameters in other classes in java

Wish I could say that I'm new, but alas I'm just extremely rusty. I'm trying to make a few simple programs to get back into the basics I learned a couple of years ago. At the moment I have two separate classes: Entity and Game. I have made a player entity object and I would like to access it's x and y parameters in different methods, and eventually different classes as well.

My first instinct was to just use 'player.x' but unfortunately that only works within the same class and only with void methods. If I try using that anywhere else I keep getting a 'NullPointerException' error on the lines where I try to reference any parameter from player. Any advice on how to reference the x and y positions without that error being thrown, or even to just know why its only being thrown in non void methods (as ideally I want to use them in a calculation in a float method) would be greatly appreciated. This is my entity class:

public class Entity {

    public float x; //x position 
    public float y; //y position

    public Entity(float x, float y){

        this.x = x;
        this.y = y;
    }
       //entity methods
}

This is my game class:

public class Game{

    public static Entity player;
    public static float posX = 2f;
    public static float posY = 2f;

    public Game(){

        player = new Entity(posX, posY);
    }  

    public static float test(){

        float newX = player.x - 2f; //I would get the error here for example
        return newX;
    }

    //Game methods

}

Thanks!

EDIT

Changed the Game class as suggested, still getting the same error.

public class Game {

public Entity player;
public float posX = 2f;
public float posY = 2f;

public float y = test();

public Game() {

    player = new Entity(posX, posY);
}

public float test() {

    float newX = player.x - 2f; //I would get the error here for example
    return newX;
}

public void print() {

    System.out.println(y);
}

public static void main(String[] args) {

    Game game = new Game();
    game.print();

}

}

Reason is simple. You are creating the player object in constructor. But using it in static method. So, your constructor is never called.

Try to make your method non-static

EDIT

You can do it two ways,

1 : make your test() method non-static and everything will work as charm.

public float test(){
    float newX = player.x -2f;
    return newX
}

and make your Entity player non-static.

2 : make your fields static and try to initialize them, before calling the test() method.

public class Entity {

public static float x; //x position 
public static float y; //y position

public Entity(float x, float y){

    this.x = x;
    this.y = y;
}
   //entity methods

public static void initialize(float tx, float ty){
    x = tx;
    y = ty;
}

public static float test(){

    float newX = Player.x - 2f; 
    return newX;
}

Ofcourse, second one is not a great solution. But a workaround.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM