0

I am making an Android app and for it I have to do some XML parsing. I followed an example, which works, but when I try to adapt the example for my usage I get a null pointer exception I don't understand. This should hold all relevant bits of code. The issue is with the line near the bottom of this snippit between the Log.e("5", "5") and the corresponding "6" with Element docEle = dom.getDocumentElement();

Any ideas?

public class Parser {

//Constructor
ArrayList<Response> responseList;
ArrayList<ResourceType> resourceTypeList;
List myEmpls;
Document dom;

public Parser()
{
    responseList = new ArrayList<Response>();
    resourceTypeList = new ArrayList<ResourceType>();
    myEmpls = new ArrayList();
}

public void addRequest(int reportId)
{
    Request request = new Request(reportId);
    Response response = new Response(request);
        //Form XML in LoadReport Request
        //Send XML
        //Receive and parse 
    parseReportResponse();
        //Input information into response
        //Form XML in GetRoomData request
        //Send XML
        //Receive and parse
        //Input information into response
    responseList.add(response);
    Log.e("5", "5");
}
public void parseReportResponse()
{
    parseReportXML();
    parseReportDocument();
}   

public void parseReportXML()
{
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder db = dbf.newDocumentBuilder();
        dom = db.parse("LoadReportResponse.xml");

    }catch(ParserConfigurationException pce) {
        pce.printStackTrace();
    }catch(SAXException se) {
        se.printStackTrace();
    }catch(IOException ioe) {
        ioe.printStackTrace();
    }
}
public void parseReportDocument()
{
    Log.e("5", "5");
    Element docEle = dom.getDocumentElement();
    Log.e("6", "6");
    NodeList nl = docEle.getElementsByTagName("Room");
    if(nl != null && nl.getLength() > 0) 
    {
        for(int i=0; i<nl.getLength(); i++) 
        {
                Element el = (Element)nl.item(i);
                //Employee e = getEmployee(el);
                Room room = getNewRoom(el);
                myEmpls.add(room);

        }
    }
}

Here's the logcat

06-24 15:16:08.209: E/AndroidRuntime(6017): FATAL EXCEPTION: main
06-24 15:16:08.209: E/AndroidRuntime(6017): java.lang.RuntimeException: Unable to start activity ComponentInfo{[Stuff I have to hide].MainActivity}: java.lang.NullPointerException
06-24 15:16:08.209: E/AndroidRuntime(6017):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-24 15:16:08.209: E/AndroidRuntime(6017):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-24 15:16:08.209: E/AndroidRuntime(6017):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-24 15:16:08.209: E/AndroidRuntime(6017):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-24 15:16:08.209: E/AndroidRuntime(6017):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 15:16:08.209: E/AndroidRuntime(6017):     at android.os.Looper.loop(Looper.java:137)
06-24 15:16:08.209: E/AndroidRuntime(6017):     at android.app.ActivityThread.main(ActivityThread.java:5039)
06-24 15:16:08.209: E/AndroidRuntime(6017):     at java.lang.reflect.Method.invokeNative(Native Method)
06-24 15:16:08.209: E/AndroidRuntime(6017):     at java.lang.reflect.Method.invoke(Method.java:511)
06-24 15:16:08.209: E/AndroidRuntime(6017):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-24 15:16:08.209: E/AndroidRuntime(6017):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-24 15:16:08.209: E/AndroidRuntime(6017):     at dalvik.system.NativeStart.main(Native Method)
06-24 15:16:08.209: E/AndroidRuntime(6017): Caused by: java.lang.NullPointerException
06-24 15:16:08.209: E/AndroidRuntime(6017):     at com.example.[I have to hide].Parser.parseReportDocument(Parser.java:81)
06-24 15:16:08.209: E/AndroidRuntime(6017):     at com.example.[I have to hide].Parser.parseReportResponse(Parser.java:59)
06-24 15:16:08.209: E/AndroidRuntime(6017):     at com.example.[I have to hide].Parser.addRequest(Parser.java:41)
06-24 15:16:08.209: E/AndroidRuntime(6017):     at com.example.[I have to hide].MainActivity.onCreate(MainActivity.java:79)
06-24 15:16:08.209: E/AndroidRuntime(6017):     at android.app.Activity.performCreate(Activity.java:5104)
06-24 15:16:08.209: E/AndroidRuntime(6017):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-24 15:16:08.209: E/AndroidRuntime(6017):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-24 15:16:08.209: E/AndroidRuntime(6017):     ... 11 more
  • 2
    can you post the Stack Trace. There could be another cause for the NullPointerException. E.g. if another exception is thrown, while you create the dom object. Then it is not initialized and it is null. Are there any previous exceptions thrown? – peshkira Jun 24 '13 at 20:29
  • What line in your code causes the NPE? – Code-Apprentice Jun 24 '13 at 20:38
  • I can't tell directly from the logcat, but once I comment "Element docEle = dom.getDocumentElement();" (and the rest below it in the function) the code no longer crashes so it should be that line. – user2471612 Jun 24 '13 at 20:41

1 Answers1

3

The problem is that you never initialize the parameter dom, because your public void parseReportXML() method is throwing an exception.

Take a look at your stacktrace and find what's the exception when you call:

dom =db.parse("LoadReportResponse.xml");

I bet my fingers that the path is not correct ;)

edit: The stacktrace you posted is telling you that db can't find the document. So, yes, the path was incorrect. Check some examples of loading a file in Java, is not that easy sometimes.

aran
  • 10,978
  • 5
  • 39
  • 69
  • Hey Asier, I don't know how to follow the stacktrace. I don't understand much of anything from that Logcat other than I'm looking at a NullPointerException. I use the same path as the example, in that I put the XML in the project folder on the same level as in the example I followed that worked. Are there additional things I need to do to make sure it's finding the file? Edit: Just saw your edit. I'll look it up. Thanks. – user2471612 Jun 24 '13 at 20:45
  • http://stackoverflow.com/questions/6639/how-should-i-load-files-into-my-java-application take a look ;) – aran Jun 24 '13 at 20:51
  • Trying to follow that I added "MainActivity.class.getResource("LoadReportResponse.xml");" in my main Activity before I construct Parser. But it still suffers from the same problem. I don't understand this, it's in the project directory, it should be able to just find it. – user2471612 Jun 24 '13 at 20:59
  • right click on the file and take a look at the `relative path` the file is given. If you do have the file correctly added to your project, then that's the path you have to insert. Tell me your progress. – aran Jun 24 '13 at 21:20
  • In Eclipse, when I right click on the file and go to Properties->Resources under Path it says "/[Something]/LoadReportResponse.xml" I tried using that instead of just LoadReportResponse.xml also but it has the exact same issue. I believe this is the path though, right? – user2471612 Jun 24 '13 at 21:27
  • yes. Did you add the file to the build path? Right click -> ADD to build path – aran Jun 24 '13 at 21:29
  • I just did. I tried that earlier also but it just give me some unknown error. As in it won't compile. It copied the file into "Referenced Libraries" but it's not a library or a class, so I figure that is the issue. Am I supposed to put it somewhere else from there? – user2471612 Jun 24 '13 at 21:32
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32298/discussion-between-asier-aranbarri-and-user2471612) – aran Jun 24 '13 at 21:40
  • Hey everyone, I was never quite able to get this working but what I did instead was put the whole XML file in the code as a string and used Uri's answer (at the bottom) to create a document out of it: http://stackoverflow.com/questions/562160/in-java-how-do-i-parse-xml-as-a-string-instead-of-a-file - Works perfectly. – user2471612 Jun 25 '13 at 18:22