Java - getClassLoader().getResource() driving me bonkers

Posted by Click Upvote on Stack Overflow See other posts from Stack Overflow or by Click Upvote
Published on 2009-03-09T10:20:20Z Indexed on 2010/06/06 2:02 UTC
Read the original article Hit count: 318

Filed under:
|

I have this test app:

import java.applet.*;
import java.awt.*;
import java.net.URL;
public class Test extends Applet
{

    public void init()
    {
    	URL some=Test.class.getClass().getClassLoader().getResource("/assets/pacman.png");
    	System.out.println(some.toString());
    	System.out.println(some.getFile());
    	System.out.println(some.getPath());

    }
}

When I run it from Eclipse, I get the error:

java.lang.NullPointerException
    at Test.init(Test.java:9)
    at sun.applet.AppletPanel.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Classpath (from .CLASSPATH file)

<classpathentry kind="src" path="src"/>

In my c:\project\src folder, I have only the Test.java file and the 'assets' directory which contains pacman.png.

What am I doing wrong and how to resolve it?

© Stack Overflow or respective owner

Related posts about java

Related posts about applet