So I am trying to load a .png file for a very simple game. I have my all of my code saved in the directory: "C:\Users\Zach\Desktop\TestGame\source", and my image that I'm trying to load in a directory called "C:\Users\Zach\Desktop\TestGame\images". To load the image i am using

ImageIcon ii = new ImageIcon(this.getClass().getResource("..\\images\\right3.png"));

I used the ".." to move up one directory where the images directory is, and I am on a Windows machine so I was told to use the "\\" as a file separator character. However when I try to run the program I get a nullpointerexception. How could I fix this problem? Thanks in advance.

Edited by zachf632: n/a

5 Years
Discussion Span
Last Post by NormR1

Try copying the image file to all the possible places that your getResource could refer to until the image is found. Then remove them one by one until it is not found.
The last removal will be where the code is looking.

Or separate the code that gets the URL from the ImageIcon constructor to a separate variable and print out its value to see where it is looking.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.