1

I have a problem with my Java code. I want to pass a file (xml file for accuracy) as argument of input to a method. I paste my code:

public File searchPlaces(){

     URL url = getClass().getResource("arml.xml");
     File file = new File(url.getPath());

     return file;
}

I have a NullPointerException when I try to call this method.

Somebody could help me?

4
  • Which line throws the null reference exception? Jan 23, 2012 at 16:07
  • Your XML file is not found (url is null I guess)
    – ndeverge
    Jan 23, 2012 at 16:07
  • log trace? which line you are getting prob?
    – Vaandu
    Jan 23, 2012 at 16:11
  • I've debugged, and the url is null. I must include the path of the file?
    – AlessioG
    Jan 23, 2012 at 16:16

3 Answers 3

2

You fail to check that the "url" is initialized with a non-null value.

Once you have a valid non-null URL, you can attempt to create a File object from it. You might have to check that the file is also non-Null (or perhaps this method is allowed to return null items). Either way, returning a null item (File or otherwise) is perfectly acceptable within Java.

De-referencing a null (calling a method on an object reference that doesn't refer to anything) is not. That's why you get the error, at

url.getPath()

You're just misidentifying where it is failing.

6
  • I've debugged, and the url is null. I must include the path of the file?
    – AlessioG
    Jan 23, 2012 at 16:15
  • It depends. You are attempting to load the file relative to a Class. If the file is packaged inside of a JAR file, this is the right way to do things. If the file is located somewhere independent of the Class's location, then you should probably ski the Class.getResource(...) and open the File directly with one of the File methods. Assuming you do want to load from a Class, then I would try again with an absolute path (from the root of the jar file), like /com/myname/mydir/arm1.xml
    – Edwin Buck
    Jan 23, 2012 at 16:17
  • It return NullPointerException, I modify my code: URL url = getClass().getResource("C:\\WebS\\test\\arml.xml");
    – AlessioG
    Jan 23, 2012 at 16:26
  • @user917651, you are trying to grab a file at a location that is not related to the location of your Class. Use new File instead of getClass().getResource(...) because the file is not at a location relative to the class.
    – Edwin Buck
    Jan 23, 2012 at 17:03
  • Could you post an example please?
    – AlessioG
    Jan 24, 2012 at 12:30
1

Yes you can. You might get a null pointer, if the new File() call generates an exception.

Debug and see if you the Url is not-null.

4
  • I've debugged, and the url is null. I must include the path of the file?
    – AlessioG
    Jan 23, 2012 at 16:14
  • Yes. URLs should have a scheme to start with. Refer to this. docs.oracle.com/javase/6/docs/api/java/net/… Jan 23, 2012 at 16:21
  • It return NullPointerException, I modify my code: URL url = getClass().getResource("C:\\WebS\\test\\arml.xml");
    – AlessioG
    Jan 23, 2012 at 16:25
  • I am not sure what exactly you are trying to do. Try this and debug. getClass().getResource("\arml.xml"); Jan 23, 2012 at 16:51
1

What you probably want to do here is not return a File object, but rather a BufferedReader or FileReader, or maybe a FileInputStream. Then, encapsulate all of your error handling within the method using a try/catch or bubble the errors up with a throws declaration.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.