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.

Jan Bodnar
  • 10,969
  • 6
  • 68
  • 77
kapitanluffy
  • 1,269
  • 7
  • 26
  • 54
  • 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? – Christoph Walesch Apr 24 '11 at 07:51
  • 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 project – kapitanluffy Apr 24 '11 at 08:02

11 Answers11

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 /

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
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.

Harry Joy
  • 58,650
  • 30
  • 162
  • 207
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.

Rohit Deshmukh
  • 130
  • 2
  • 6
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.

MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
2

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

jumperchen
  • 1,463
  • 12
  • 27
0

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

vara prasad
  • 119
  • 1
  • 1
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>

maphysics
  • 31
  • 3
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.

developJadon
  • 39
  • 10
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"));
RamPrakash
  • 1,687
  • 3
  • 20
  • 25
Muhammad Ali
  • 179
  • 1
  • 10
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).

Anil G
  • 187
  • 1
  • 3
  • 11
  • 1
    Thse two sentences re mutually contradictory. Using the class loader method loses the information about where the .class file is. – user207421 Jan 20 '14 at 03:32
-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.

user207421
  • 305,947
  • 44
  • 307
  • 483