tags:

views:

5405

answers:

3

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?

+6  A: 

You don't need the slash at the start when getting a resource from a ClassLoader, because there's no idea of a "relative" part to start with. You only need it when you're getting a resource from a Class where relative paths go from the class's package level.

In addition, you don't want Test.class.getClass() as that gets the class of Test.class, which will be Class<Class>.

In other words, try either of these lines:

URL viaClass=Test.class.getResource("/assets/pacman.png");
URL viaLoader=Test.class.getClassLoader().getResource("assets/pacman.png");
Jon Skeet
Thanks. I tried both but it doesn't work, however now it gives an exception on line 10 (which is the first system.out.println line) instead of line 9. I've made sure and the image is in project/src/assets...
Click Upvote
that measn that "some" is null... which means you are not getting the resource properly.
TofuBeer
yes, so how can i resolve it? is the classpath incorrect or something else i need to change?
Click Upvote
Try getting the applet side of things out of the equation - run the same code in a console app.
Jon Skeet
But i'm building an applet so I could run this in a web browser, it would be pointless to make it a console :(. There must be some way to make it work in the applet? How do applets use images?
Click Upvote
No, it wouldn't be pointless to make it a console app - it would solve one problem at a time. Make sure you can get a resource in a simple setting, then move on to a more complicated one. For instance, your original code would have failed even in a console app.
Jon Skeet
Ok, i removed all references to applet and changed init method to public static void main(String args[]). Got the error : "Exception in thread "main" java.lang.NullPointerException at Test.main(Test.java:10)"
Click Upvote
Same output for both of your examples
Click Upvote
Glad you've got this fixed now. The technique of "use a simpler context to check something" is an important one though.
Jon Skeet
Thanks, your code helped equally
Click Upvote
+3  A: 

I would do it this way:

final InputStream stream;

stream = Test.class.getResourceAsStream("assets/pacman.png");
System.out.println("Stream = " + stream);

"/assets/pacman.png" is an absolute location whle "assets/pacman.png" is a relative location.

TofuBeer
Your code gives the output "Stream = null" :(
Click Upvote
as expected, given that you other attempts didn't work. What is the package that the classes are in, where is the .class file located?
TofuBeer
The .class is put in c:\java\test\bin when i hit run in eclipse. The .java is in c:\java\test\src and the assets/ folder is also in this dir. No package being used right now because im just trying to make it work for my other project. Thanks
Click Upvote
the assests dir needs to be where the .class file is
TofuBeer
glad you got it working
TofuBeer
A: 

Mr. Skeet, Please help!!! You suggested to TofuBeer that he use:

URL viaClass=Test.class.getResource("/assets/pacman.png");
URL viaLoader=Test.class.getClassLoader().getResource("assets/pacman.png");

But I have tried this type of thing and it's not working. I get the error "URI is not hierarchical" My code:

URL url = new URL(adminProps.getProperty(MyProperties.CUSTOMER_PROFILE_FORM_URL.getPropertyName()));
System.out.println("URL: " + url);
URI uri = url.getClass().getResource("/forms/PSS-F-00045-R01-CP.pdf").toURI();
System.out.println("URI: " + uri);
File cpForm = new File(uri);

My error:

URL: http://192.168.102.239/IncidentReportViewer/forms/PSS-F-00045-R01-CP.pdf
URI: jar:file:/home/kkennedy/NetBeansProjects/IRUtilities/dist/IRUtilities.jar!/forms
java.lang.IllegalArgumentException: URI is not hierarchical
/PSS-F-00045-R01-CP.pdf
        at java.io.File.<init>(File.java:363)
        at emailhandlingpackage.SendEmails.sendEmailToCustomer(SendEmails.java:113)   

Of course, the first 2 lines are from the System.out, but I can't figure out how to get this to work. I assumed that the problem was because I've got this in a static method and so I couldn't use "this" for reference. But it appears that you're suggesting that itsn't a problem. Any suggestions would be much appreciated. Thanks.

Kit