Skip to content
Advertisement

How to fix try/catch Java NullPointerException

I am trying to create a 2D platform game, by following this tutorial. I have created an Images()-class which looks like this: package Resources;

JavaScript

And I created an instantation of it in my GamePanel()-class, which looks like this

JavaScript

I get an error saying

JavaScript

And when I try to run the game it doesn’t launch, I only get a NullPointerException

JavaScript

But if I remove the the instantation, the game launches, but then it crashes with even more errors

JavaScript

If I remove the Objects.requireNonNull from tileBlocks[0] = ImageIO.read((getClass().getResourceAsStream("TileBlocks/block_brick.png"))

The game launches, but it crashes and I get another error saying

JavaScript

What am I doing wrong here? Why is my list of tileBlocks null? How can I read the image and display it on the tileBlocks?

Advertisement

Answer

Put file “block_brick.png” in directory “TileBlocks” in classpath.

Advertisement