Java getClass().getResource("file") leads to NullPointerException
Asked Answered
J

11

16

I am following the Snake Java games tutorial and always get this error:

ImageIcon iid = new ImageIcon(this.getClass().getResource("ball.png"));
ball = iid.getImage();

Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at snake2.Board.<init>(Board.java:52)
    at snake2.Snake.<init>(Snake.java:10)
    at snake2.Snake.main(Snake.java:22)

I actually just copied and pasted the code to see how it works. They are in the right packages too; but when I try to run it, I always end up with this error.

Jackquelinejackrabbit answered 24/4, 2011 at 7:48 Comment(2)
Is there a ball.png file next to the .java file? In case you're using eclipse, did you refresh the source folder? Is the code above called from a subclasses method in a different package?Anesthesiology
i imported it in the wrong place .i thought importing it in the project adds it in the resources. how about adding resources globally like i want to add D:\myresources\ to my projectJackquelinejackrabbit
N
15

The image should be in the same package (folder in OS terms) as the compiled class. Check whether you have both .class and .png in the same folder. If not, you can use classpath-relative paths in getResource(..), by starting with /

Neale answered 24/4, 2011 at 7:53 Comment(0)
T
8

Try this:

ImageIcon iid = new ImageIcon(this.getClass()
                  .getClassLoader().getResource("ball.png"));
ball = iid.getImage();

Make sure image is in the same folder as java file.

Thaw answered 24/4, 2011 at 7:53 Comment(0)
L
6

Try using System.out.println(System.getProperty("java.class.path")); to find out location of your .class file and place the images in this folder.

Libradalibrarian answered 24/5, 2013 at 14:56 Comment(0)
M
4

It is general risky to load resources using relative paths, I'd always recommend using absolute paths, so do

 /ball.png

if the the image is at the root of your classpath, or add a path to the location.

Monk answered 24/4, 2011 at 7:51 Comment(0)
S
2

You have to put the image file(ball.png) into your classpath. More details, please take a look at the Javadoc.

Stress answered 24/4, 2011 at 7:53 Comment(0)
O
0

if the resource is in your classpath then you should be trying "this.getClass().getClassLoader().getResource("ball.png")". For you actual code to work, the ball.png needs to be in the location where your .class file is (i.e., inside the package).

Ossifrage answered 24/4, 2011 at 7:51 Comment(1)
Thse two sentences re mutually contradictory. Using the class loader method loses the information about where the .class file is.Maid
B
0

Go to project >clean in the eclipse it would refresh the package explorer and you won't face this problem anymore.

Brandy answered 25/6, 2015 at 6:31 Comment(0)
C
0

You may need to add the file to your build resources, something like this:

<build>
    <resources>
        <resource>
            <directory>path\to\resources</directory>
            <includes>
                <include>ball.png</include>
            </includes>
        </resource>
    </resources>

Caspian answered 7/11, 2016 at 20:27 Comment(0)
U
0

You can use only path of your image. I think this will help you: Use this:

ImageIcon iid = new ImageIcon("C:\\Users\\ranig\\My\\spaceinvaders\\ball.png");

Note: C:\\Users\\ranig\\My\\spaceinvaders\\ball.png is the whole path of ball.png image.

instead of this:

ImageIcon iid = new ImageIcon(this.getClass().getResource("ball.png"));

Note: If u want to only try snake code and only want to get output.

Unseat answered 8/11, 2016 at 10:48 Comment(0)
K
0

I will make it simple for you . Here is an example:

Icon bug = new ImageIcon(getClass().getResource("bug1.png"));

here "bug1.png" is the resource and if it is unavailable then it can cause error as you have discussed here.

Import an image to the same directory in which your program resides.

You can also give whole path to it as well

ImageIcon(getClass().getResource("C://me/file/bug1.png"));
Kebab answered 20/2, 2017 at 19:48 Comment(0)
M
-2

The resource so named wasn't found. It needs to be in the same directory as the .class file you are calling it from. See the Javadoc.

Maid answered 24/4, 2011 at 7:50 Comment(2)
so by that you mean i should add the ball.png to the resource?how?Jackquelinejackrabbit
@kapitanluffy: You seem to be using the resources API without knowing the first thing about what it's for. I suggest you read the Javadoc.Maid

© 2022 - 2024 — McMap. All rights reserved.