1

I have attempted about everything I have seen regarding this issue on stackoverflow but can't seem to correctly retrieve an xml file using the .getClass method. I am continuing to get a null pointer exception but can't figure out why. I have tried several variations. If I am doing anything wrong please let me know. Also, if you require any additional information just ask :D. Here are some of the variations I have tried

URL configURL = new URL("" + Transcriber.class.getClass().getClassLoader().getResourceAsStream("/config.xml"));
ConfigurationManager cm = new ConfigurationManager(configURL);

URL configURL = new URL("" + Transcriber.class.getClass().getClassLoader().getResource("/config.xml"));
ConfigurationManager cm = new ConfigurationManager(configURL);

InputStream configURL = Transcriber.class.getClass().getClassLoader().getResourceAsStream("/config.xml");
ConfigurationManager cm = new ConfigurationManager(configURL.toString());

I should also note that when using eclipse I am able to retrieve the xml. However, when converted to an executable jar I cannot.

EDIT:

URL configURL = new URL("" + Transcriber.class.getResourceAsStream("config.xml"));

Console output =

Exception in thread "main" java.net.MalformedURLException: no protocol: java.io.BufferedInputStream@238be9f2
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at java.net.URL.<init>(Unknown Source)
    at speechcapture.Transcriber.main(Transcriber.java:53)
4
  • 1
    Where is the config.xml resource in the jar? Commented Apr 14, 2014 at 6:26
  • It is located in the same package folder with Transcriber.class (bin/speechcapture/) Commented Apr 14, 2014 at 6:29
  • A previous post explains this problem perfectly stackoverflow.com/questions/1119740/… Commented Apr 14, 2014 at 6:32
  • @AurA I seem to have attempted the approaches listed in the link unless I missed one when reading over the answers provided. Commented Apr 14, 2014 at 6:55

1 Answer 1

2

Your first problem is that

Transcriber.class.getClass()

returns the Class object for the Class class. What you wanted was

Transcriber.class

The second problem is that, assuming your config.xml resource is in the same package as the Transcriber class, then you need to either provide the fully qualified path to the resource or get it relative to the Transcriber class' package. So either

URL configURL = Transcriber.class.getResource("config.xml");

or

URL configURL = Transcriber.class.getResource("/com/example/config.xml");
Sign up to request clarification or add additional context in comments.

10 Comments

I have tried URL configURL = new URL("" + Transcriber.class.getResourceAsStream("config.xml")); as well and config.xml is in the same package as the Transcriber class.
@user2916286 Both of the entries in my answer will work if both the class Transcriber and the config.xml file are in the same package. Note how the URL is constructed.
I added it as written but I am still seeing the error null pointer exception. But with adding your's I am now seeing a malformed URL exception.
@user2916286 Post the exact one you're using and the full stack trace. Do this in an edit your question.
@user2916286 Note the difference between my code and yours. Why are you constructing your URL like that?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.