Help using extends

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 ...
}

You are using variables out of scope. The private variables that you declare in the Actor object are only available within the class, not to any object that inherits the Actor class.

When you declare the same variables in the Fighter object, they are different variables even though they are named the same. It then gets a null pointer because the variables in the Actor class are never set.

Just change the variables in the Actor class to be public and remove the variables from the Fighter class as follows:

public class Fighter extends Actor{
 
    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{
    public Node node, target;
    public int x, y, width, height;
    public Image image;
    public String src;
    public double speed;
    public ArrayList<Node> path;
    public ArrayList<Node> open;
    public ArrayList<Node> closed;
 
    public Actor(){
    }
 
        ... insert methods here ...
}

Better yet: protected :wink:

2 few thing :

  • You can load image via ImageIO

  try
  {
     image = ImageIO.read(this.getClass().getResource(src));
  }
  catch(Exception ex) // Uggly use the right exception
  {
     // Image not loaded
  }

  • You reload the image everytime you create a fighter. You can use “static” member to have a member commun to all objects of a class. With this code, you load the image only one time :
public class Fighter extends Actor{
    private static final String SRC = "/resources/fighter.gif";
    private static Image fighterImage = null;

    public Fighter(Node node){
        System.out.println("MAR");
        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();
         
        if(fighterImage == null)
       {
          try
          {
             fighterImage = ImageIO.read(this.getClass().getResource(SRC));
          }
          catch(Exception ex) // Uggly use the right exception
          {
             // Image not loaded
          }
       }

        image = fighterImage;
         
        width = image.getWidth(null);
        height = image.getHeight(null);
    }
}
 
public class Actor{
    public Node node, target;
    public int x, y, width, height;
    public Image image;
    public String src;
    public double speed;
    public ArrayList<Node> path;
    public ArrayList<Node> open;
    public ArrayList<Node> closed;
 
    public Actor(){
    }
 
        ... insert methods here ...
}

package protected (no modifier) is underrated =D

I am so absolutely appalled with myself.

Completely and totally disgusted.

Thank you so much!

But… it looks weird…

<.<

.>

Also I find it hard to justify package protected…
When is it actually good code to use it? (Stop me if I’m going off road here)

  • Scarzzurs

I don’t know whether or not it’s good code, but say you have a package that holds all the Entitys for your game. Maybe you want the Entitys to be able to access methods and fields that the rest of the world shouldn’t be able to access. Then it’s useful.

EDIT: I worded that a little weird, allow me to clarify. “Maybe you want the Entities to access methods and variables of other Entities that the rest of the world shouldn’t have access to.”

Yeah, a method/variable that you only want another class in the same package to access.