Hello, I’m hoping I can get some help with this…
I have several different objects who all share the same code. I am originally coding each object separately but find the fact that I am writing out the exact same methods in each object to be frustrating. I tried compiling all the methods in a superclass Actor which the other classes extends but I am getting a null pointer exception error when I attempt to access a field of the subclass.
Here is the subclass code Fighter which extends Actor. Actor contains all the getters and setters. The default constructor for the Actor method does nothing currently. Can you tell me what I’m doing wrong?
public class Fighter extends Actor{
private Node node, target;
private int x, y, width, height;
private Image image;
private String src;
private double speed;
private ArrayList<Node> path;
private ArrayList<Node> open;
private ArrayList<Node> closed;
public Fighter(Node node){
System.out.println("MAR");
src = "/resources/fighter.gif";
path = new ArrayList<Node>();
open = new ArrayList<Node>();
closed = new ArrayList<Node>();
this.node = node;
this.node.setActor(this);
target = null;
speed = 2;
x = node.getCentreX();
y = node.getCentreY();
ImageIcon ii = new ImageIcon(this.getClass().getResource(src));
image = ii.getImage();
width = image.getWidth(null);
height = image.getHeight(null);
}
}
public class Actor{
private Node node, target;
private int x, y, width, height;
private Image image;
private String src;
private double speed;
private ArrayList<Node> path;
private ArrayList<Node> open;
private ArrayList<Node> closed;
public Actor(){
}
... insert methods here ...
}