0

New to Java.

I have an instance player1 of the Player class below.

Player player1 = new Player(0,0);

Inside the Player class I have composed an object coordinate of type Coord (defined below). When I instantiate player1 above "Player is at coordinate 0,0" is displayed as expected.

public class Player extends Entity { 
    public Coord coordinate;
    public Player(int x, int y) {
        Coord coordinate = new Coord(x,y);
        System.out.println(“Player is at coordinate “ + coordinate.getX() + “,” 
        + coordinate.getY());
    }
}

The Coord class is defined as follows.

public class Coord {
    private int x;
    private int y;

    public Coord(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }
}

The problem arises when I try to access obj and its respective methods after I instantiate player1. When I try to access coordinate I get a NullPointerException error.

Player player1 = new Player(0,0);

System.out.println(“Player is at coordinate “ + player1.coordinate.getX() + 
“,” + player1.coordinate.getY());

What am I doing wrong?

Ray Rackiewicz
  • 303
  • 1
  • 3
  • 12

1 Answers1

1

You aren't making Coord obj; a field of your class. That could be as simple as something like

public class Player extends Entity { 
    Coord obj;
    public Player(int x, int y) {
        obj = new Coord(x,y);
        System.out.println(“Player is at coordinate “ + obj.getX() + “,” 
        + obj.getY());
    }
}

Note that obj is a terrible field name, and that it has default level access permission here. One way to improve that might be something like

public class Player extends Entity { 
    private Coord coordinates;
    public Player(int x, int y) {
        coordinates = new Coord(x,y);
        System.out.println(“Player is at coordinate “ + obj.getX() + “,” 
        + obj.getY());
    }
    public Coord getCoordinates() {
        return coordinates;
    }
}

Then you could use it like

Player player1 = new Player(0,0);

System.out.println(“Player is at coordinate “ 
    + player1.getCoordinates().getX() 
    + “,” + player1.getCoordinates().getY());

You might also override toString() in the Coord class, then you could say

System.out.println(“Player is at coordinate “ 
    + player1.getCoordinates());
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I failed to mention that obj is defined as a field in my class. Would it matter if obj was defined as a field in the superclass? Also, I only used obj for simplicity to post on here. – Ray Rackiewicz Apr 07 '15 at 00:57
  • @RaymondRackiewicz Post **real** code that exhibits the problem. Based on what you've told us, you're [shadowing the variable](http://en.wikipedia.org/wiki/Variable_shadowing). – Elliott Frisch Apr 07 '15 at 00:59
  • It would matter if it was __private__ in the superclass – D. Ben Knoble Apr 07 '15 at 00:59
  • Problem solved. I had the type listed in front of the variable coordinate even though I had already declared it as a field. A stupid rookie mistake! Thanks for getting me to think about the problem. – Ray Rackiewicz Apr 07 '15 at 01:14