2

I have code that is running correctly on a development machine but throws a NullPointerException when installed in a production environment. The line that throws the exception is the following:

MyClass.class.getClassLoader().getResource("").getPath();

So I split this into multiple lines of code to see exactly which call was returning null, like this:

ClassLoader cl = MyClass.class.getClassLoader();
URL url = cl.getResource("");
String path = url.getPath();

Now the url.getPath() call is throwing the NullPointerException, meaning that cl.getResource("") is returning null.

Can anyone tell me how this call can ever return null?

Matthias
  • 3,582
  • 2
  • 30
  • 41
Zak
  • 311
  • 2
  • 5
  • 12
  • you need to put in `getResource("absolute path for file")`? – Husam Oct 11 '13 at 09:20
  • For the reasons stated in the Javadoc. What part of that don't you understand? – user207421 Oct 11 '13 at 09:23
  • Your code is doing what is expected. What would you expect to get if you are searching for the resource with name/path ""? – Matthias Oct 11 '13 at 09:25
  • @Matthias and EJP When getResource("") is called on the class loader, it returns the URL of the root directory of the java application. At least this is how it is currently working on my development machine. – Zak Oct 11 '13 at 09:46
  • 2
    @Zak Well, then this is what the ClassLoader of your local execution environment is doing. This is however specific to the `ClassLoader`. Expect other ClassLoaders to behave differently (especially if you execute that code inside a Application Server, WebStart Launcher or any environment that has some security restrictions). – Matthias Oct 11 '13 at 09:48
  • @Matthias this is the correct answer. It turns out I was using Sun JDK 1.5.0_12 on my development environment while the default JRE in production machine was set to jre-1.4.2-gcj. Please write this up in an answer and I will be glad to mark it as the correct one. Thanks! – Zak Oct 11 '13 at 10:00

4 Answers4

3

It's clearly said in the javadocs for the ClassLoader#getResource(String name) method.

Returns: A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource.

Obviously, there is no resource with name "" and therefore null is returned.;

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
  • I have tested this several times on my development machine and it returns the root directory of my application. It is easy to test. You can try it on your machine. – Zak Oct 11 '13 at 09:41
3

The implementation of getResource is different for different ClassLoader implementations.

While this might reliably work on your local machine, it is not guaranteed to be successful on other ClassLoader implementations.

So expect other ClassLoaders to behave differently (especially if you execute that code inside a Application Server, WebStart Launcher or any environment that has some security restrictions).

Matthias
  • 3,582
  • 2
  • 30
  • 41
0

The url is null, so you can't call getPath() on it. Calling getResource with a "" parameter will, at in my understanding always return null, so this is guaranteed to blow up.

Waldheinz
  • 10,399
  • 3
  • 31
  • 61
0

getClassLoader public ClassLoader getClassLoader() Returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader.

Read above lines.

Ankit Jain
  • 2,230
  • 1
  • 18
  • 25