0

I am having a weird error, I am using a variable that I create but then I makes a Null Pointer Exception Error. Here is my code :

public class ReadXMLFile {
// Initialize logger
private static Logger log = Logger.getLogger(ReadXMLFile.class);

public Document getXMLDocument() {
    Document doc = null;

    File configXmlFile = new File("");
    try {           
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

        System.out.println(LanguageHandler.source); //null

        configXmlFile = LanguageHandler.source;

        System.out.println(LanguageHandler.source); //null
        System.out.println("conf : " + configXmlFile.isFile()); //Null pointer exception

        doc = docBuilder.parse(configXmlFile);  

    } catch (Exception e) {
        e.printStackTrace();
        log.error(e.getMessage(), e);
    }

    return doc;
}

}

And here is the error :

java.lang.NullPointerException
at in.raster.oviyam.util.ReadXMLFile.getXMLDocument(ReadXMLFile.java:85)
at in.raster.oviyam.util.ReadXMLFile.getElementValues(ReadXMLFile.java:99)
at in.raster.oviyam.servlet.DicomNodes.doGet(DicomNodes.java:90)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Line 85 of the ReadXMLFile class is my System.out.println("conf : " + configXmlFile.isFile());

I don't understand why I am having this error, it should just return false.

Thanks in advance for help !!

V.

2
  • A null pointer exception means you're trying to access something that doesn't exist. That very likely means that configXmlFile wasn't created / instantiated properly. Check that first. Commented Aug 29, 2018 at 15:29
  • If configXmlFile is null as you have suggested through the program output, how are you expecting your program to skip a NullPointerException? Commented Aug 29, 2018 at 15:31

3 Answers 3

1

According to your code and comments System.out.println(LanguageHandler.source); //null which means that LanguageHandler.source actually is null. When you then assign this value in configXmlFile = LanguageHandler.source; your property configXmlFile also has null value. And when you call method on null reference you get NPE. So you need to check your code to properly initialize LanguageHandler.source variable.

Sign up to request clarification or add additional context in comments.

Comments

1

You are calling an object method isFile() on a an object, that is null.

You assign

configXmlFile = LanguageHandler.source;

And in the previous line you see, that the output of

System.out.println(LanguageHandler.source);

is null. So, configXmlFile is also null, since you assigned it the reference to LanguageHandler.source. Therefore configXmlFile.isFile() results in a NullPointerException.

Comments

1

You can't call methods on a NULL value.

You set it

File configXmlFile = new File("");

And then reassign the value

configXmlFile = LanguageHandler.source;

You have already confirmed that LanguageHandler.source is null, so configXmlFile is now also null.

If you expect a valid object, resume your search for the problem in LanguageHandler.source.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.