开发者

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

开发者 https://www.devze.com 2023-02-28 14:43 出处:网络
I am following the Snake Java games tutorial and always get this error: ImageIcon iid = new ImageIcon(this.getClass().getResource("ball.png"));

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>(U开发者_StackOverflow社区nknown 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.


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


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消