Java Parent class receives null on BufferedImage

I am trying to create multiple doors from the parent Door and Grandparent Objects. Every variable has the value I expect it to have EXCEPT the BufferedImage ones. For some reason every BufferedImage after IronDoor receives null. It does have the value I want it to have in IronDoor. Closed, padlocked and open are all null. So I get why drawThis is null, but why the others too? What am I doing wrong with BufferedImage?
This is my code:
public class Objects {
public boolean collision = true;
public int x, y, width, height;
public Rectangle solidArea;
public int tileSize, solidAreaX, solidAreaY, solidAreaWidth, solidAreaHeight;
protected ScreenPosition position;
protected BufferedImage drawThis;
public Keyboard keyboard;
public Objects() {
this.position = new ScreenPosition();
Screen screen = new Screen();
this.tileSize = 48;
this.width = tileSize;
this.height = tileSize;
this.solidAreaX = 0;
this.solidAreaY = 0;
this.solidAreaWidth = tileSize;
this.solidAreaHeight = tileSize;
this.solidArea = new Rectangle(solidAreaX, solidAreaY, solidAreaWidth, solidAreaHeight);
}
}
public class Door extends Objects {
public boolean locked = true, opened = false;
BufferedImage closed, padlocked, open;
Image image = new Image();
public Door () {
this.drawThis();
this.height = tileSize * 2;
}
private void drawThis() {
if(locked) {
opened = false;
super.drawThis = padlocked;
}
else if(opened) {
locked = false;
super.drawThis = open;
}
else {
super.drawThis = closed;
}
}
}
public class IronDoor extends Door {
public IronDoor(Keyboard keyboard, int x, int y, boolean locked) {
super.closed = image.getImage("/Objects/iron door closed.png");
super.padlocked = image.getImage("/Objects/iron door locked.png");
super.open = image.getImage("/Objects/iron door open.png");
super.keyboard = keyboard;
super.x = x;
super.y = y;
super.locked = locked;
}
}
//This is outside the classes:
ArrayList<Objects> objects = new ArrayList<Objects>();
objects.add(new IronDoor(keyboard, 180, 190, true));
objects.add(new IronDoor(keyboard, 300, 190, false));
//And this is where I make the image:
//I've been able to draw every other object I wanted with this class. But those objects weren't part of an inheritance.
public class Image {
public BufferedImage getImage(String path) {
BufferedImage image = null;
try {
image = ImageIO.read(getClass().getResourceAsStream(path));
}
catch (IOException e) {
e.printStackTrace();
}
return image;
}
}
Answer
Ensure following files exists in the same location that files for working components are:
- "/Objects/iron door closed.png"
- "/Objects/iron door locked.png"
- "/Objects/iron door open.png"
Note that leading slash is meaningful.
Enjoyed this question?
Check out more content on our blog or follow us on social media.
Browse more questions