简体   繁体   中英

Java getClass().getResource("file") leads to NullPointerException

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.

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 /

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.

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.

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.

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

在 Eclipse 中转到 project >clean 它将刷新包资源管理器,您将不再遇到此问题。

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>

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.

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

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 (ie, inside the package).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM