Search code examples
androidparsingrss-reader

Android - null object reference, Parsing RSS file


Can anyone help me to understand this error? I try to parse a xml file and get the content. But everytime i get this error. There are many people with a similar error but nothing helped me yet.

08-17 10:34:18.589 25213-25567/com.example.entwicklung1.designtestapp E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
                                                                                    Process: com.example.entwicklung1.designtestapp, PID: 25213
                                                                                    java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                                        at android.os.AsyncTask$3.done(AsyncTask.java:309)
                                                                                        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                                        at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                                        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                                        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                                        at java.lang.Thread.run(Thread.java:818)
                                                                                     Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'org.w3c.dom.NodeList org.w3c.dom.Node.getChildNodes()' on a null object reference
                                                                                        at com.example.entwicklung1.designtestapp.ReadRssZander.ProcessXml(ReadRssZander.java:66)
                                                                                        at com.example.entwicklung1.designtestapp.ReadRssZander.doInBackground(ReadRssZander.java:58)
                                                                                        at com.example.entwicklung1.designtestapp.ReadRssZander.doInBackground(ReadRssZander.java:29)
                                                                                        at android.os.AsyncTask$2.call(AsyncTask.java:295)
                                                                                        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                                                                                        at java.lang.Thread.run(Thread.java:818)

Here is a part of my code. I can not even look for childitems with "item". I get the error already here:NodeList items = channel.getChildNodes();

@Override
protected Void doInBackground(Void... params) {
    ProcessXml(Getdata());
    return null;
}

private void ProcessXml(Document data) {
    if(data!=null) {
        Element root = data.getDocumentElement();
        Node channel = root.getChildNodes().item(1);
        NodeList items = channel.getChildNodes();

        //for(int i=0;i<items.getLength();i++){
        //    Node currentchild = items.item(i);
        //    Log.d("textcontent",currentchild.getTextContent());
           // if(currentchild.getNodeName().equalsIgnoreCase("item")){
           //     NodeList itemchilds=currentchild.getChildNodes();
           //     for(int j=0;j<itemchilds.getLength();j++){
           //         Node current= itemchilds.item(j);
           //         Log.d("textcontent",current.getTextContent());
           //     }
           // }
        //}
    }
}

public Document Getdata(){
    try {
        url = new URL(adress);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        InputStream inputStrem = connection.getInputStream();
        DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
        DocumentBuilder builder=builderFactory.newDocumentBuilder();
        Document xmlDoc =builder.parse(inputStrem);
        return xmlDoc;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

This ist the XML file, I try to parse: Link


Solution

  • I found a solution for my problem. I was looking for a wrong item.

    The correct item i need is Node channel = root.getChildNodes().item(0); instead of Node channel = root.getChildNodes().item(1);