Java getClass().getResource(“file”) leads to NullP

2019-04-19 09:46发布

问题:

I am following the zetcode 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.

回答1:

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 /



回答2:

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.



回答3:

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.



回答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.



回答5:

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



回答6:

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).



回答7:

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



回答8:

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>



回答9:

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.



回答10:

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"));


回答11:

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.