简体   繁体   English

在Java中使用DOM解析器时出现Null异常

[英]Null Exception when using DOM parser in Java

I am making an Android app and for it I have to do some XML parsing. 我正在制作一个Android应用程序,为此我必须进行一些XML解析。 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(); 问题在于此代码段底部附近的Log.e("5", "5")Element docEle = dom.getDocumentElement();的相应"6" 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 这是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

The problem is that you never initialize the parameter dom , because your public void parseReportXML() method is throwing an exception. 问题是您永远不会初始化参数dom ,因为您的public void parseReportXML()方法会引发异常。

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. 编辑:您发布的stacktrace告诉您db找不到文档。 So, yes, the path was incorrect. 因此,是的,路径不正确。 Check some examples of loading a file in Java, is not that easy sometimes. 检查一些用Java加载文件的示例,有时并不容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM