0

So I was attempting to upload some images to a buffered image array in a class, but when I run the code, the class that I have the code in returns an npe. The image is a png file. Here's the code:

...

private BufferedImage[] img = new BufferedImage[2];

public Backdrop(String args[]) {
    for (int i = 1; i <= 2; i++) {
        String path = "resources/Map1"+i+".png";
        File file = new File(path);

        if (file.length() <= 0 || !file.isFile() || !file.exists()) {
            System.out.println("Image " + i + " failed first test");
        }

        try {
            FileInputStream fis = new FileInputStream(file);
            img[i-1] = ImageIO.read(fis);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (img[i-1] == null) {
            System.out.println("Failed to upload Map1" + i + ".png to array");
        }
    }
}

public void render(Graphics g){
    switch(currentBackdrop) {
        case Map11:
            /*//upload map image
            g.drawImage(img, 0, 0, null);*/
            break;
        case Menu:
            g.setColor(Color.black);
            g.fillRect(0, 0, WIDTH, HEIGHT);
            break;
        default:
            break;
    }
}

As the title says, I've already tried using a fileinputstream, a imageinputstream, a bufferedinputstream. I've also tried using new File(...). Same thing happens. I'm thinking it's the img[i-1] = ImageIO.read(fis); because when I comment it out everything works fine (obviously the image doesn't display because I've commented the g.drawImage code out and the array is empty). Before anything is said about the fact that there's only a small number inside the for loop thing, I am going to increase it later on, I'm just trying to get this to work first. I'm pretty sure the path is in the correct location because if I change the path to something like fdilsjah.png, the error would be a filenotfoundexception. Also, the first println code doesn't run, so I think that also shows that file exists.

Here's the error messages:

Exception in thread "Thread-0" java.lang.NullPointerException
    at com.CASGame.InsomniacCheese.Game.render(Game.java:137)
    at com.CASGame.InsomniacCheese.Game.run(Game.java:85)

so Game.java:137 is just backdrop.render(g) (which is just a private instance of the Backdrop class)

  • Are you referring to the backdrop.render(g) part of my code or the imageio part? If it's the backdrop.render(g) part, I've initialized it by creating a new instance of the class. If it's the imageio part, I think I've initialized the Buffered image array at the start of class, and I've initialized the file and fileinputstream in the constructor. After initializing the array, I tried to add values to the array, but that part of code, I think, breaks down and sends the npe – LeavesAreTheBest Jan 08 '22 at 07:28
  • First you need to figure out which reference is `null`. The stack trace tells you that it's happening at line 137 of Game.java, in the render function. – tgdavies Jan 08 '22 at 09:27
  • Don't do `} catch (IOException e) { e.printStackTrace(); }`, let the exception be thrown to the top level. You are masking the real problem. – tgdavies Jan 08 '22 at 09:28
  • I think I just did that. That's when you have the throw line in the constructor right? Nothing changed – LeavesAreTheBest Jan 09 '22 at 04:44

1 Answers1

0

If you have backdrop.render(g) at Game.java:137, the aforementioned stack trace indicates that backdrop is null.

Vladimir Korenev
  • 1,124
  • 1
  • 9
  • 27
  • I thought that would be the case, but I'm not sure how the ImageIO.read thing relates to that, because if I take that part of the code out, everything in that backdrop runs fine – LeavesAreTheBest Jan 08 '22 at 08:33
  • If you look at the stack trace, the NPE is inside `render`. – tgdavies Jan 09 '22 at 03:14
  • Yeah, but even if I comment out the entirety of the render method, it still does the npe. I'm pretty sure it's the backdrop variable because when i system.out.println it with the imageio.read thing the output is null. If I take it out, the output is no longer null, but the actual class' id and stuff. – LeavesAreTheBest Jan 09 '22 at 04:15
  • I was thinking that the issue here is maybe the ImageIO.read thing doesn't let the constructor finish running, but I'm not sure why or how to fix it. – LeavesAreTheBest Jan 09 '22 at 04:43