In my program, I'm trying to set x and y values to game.getWidth() so that I can access those variables elsewhere. The issue is that when I run the program those variable declarations throw a NullPointerException. I'm not sure exactly why these are causing an error. Is there a way I can fix it?
package data;
import java.awt.Color;
import java.awt.Graphics2D;
public class Star {
private float mass;
public Star(Game game) {
this.game = game;
}
private Game game;
//These variables cause the NullPointerException
private int x = game.getWidth() / 2;
private int y = game.getHeight() / 2;
public void paint(Graphics2D g) {
g.setColor(Color.WHITE);
g.fillOval(x, y, 12, 12);
g.setColor(Color.BLACK);
}
public float getMass() {
return mass;
}
public void setMass(float mass) {
this.mass = mass;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}