简体   繁体   中英

null pointer exception on getNodeValue() when parsing XML web page - android

I looked over several of the answers posted here, but I can't find the answer I need. It may have to do with the web site itself, but I don't think it is. I'm trying to parse an XML on a web site and I'm getting a null pointer exception error.

I run the parsing is a separate thread following Google demand when reading from the web.

please see my code and try to help.

class BackgroundTask1 extends AsyncTask<String, Void, String[]> {

protected String[] doInBackground(String... url) {
    new HttpGet();
    new StringBuffer();
    InputStream is = null;
    HttpURLConnection con = null;

    try {
        //Log.d("eyal", "URL: " + boiUrl);
        URL url1 = new URL("http://www.boi.org.il/currency.xml");
        con = (HttpURLConnection)url1.openConnection();
        con.setRequestMethod("GET");
        con.connect();
        is = con.getInputStream();
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(is);
        NodeList lastVld = doc.getElementsByTagName("LAST_UPDATE");
        String lastV = lastVld.item(0).getFirstChild().getNodeValue();
        }

        catch (Exception e) {
           e.printStackTrace();
        }

I get the error on the last line.

Thanks for your help.

You only have one LAST_UPDATE tag in your xml and it has an inner value, so try just using the node value from the Node Class you get from item(0)

 String lastV = lastVld.item(0).getNodeValue();

HTHs

There is no node returned for that tag name. You may want to first check the size of the lastVld and then try to access the items in there.

This code worked for me

InputStream is = null;
HttpURLConnection con = null;

try {
    URL url1 = new URL("http://www.boi.org.il/currency.xml");
    con = (HttpURLConnection)url1.openConnection();
    con.setRequestMethod("GET");
    con.connect();
    is = con.getInputStream();
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(is);
    NodeList lastVld = doc.getElementsByTagName("LAST_UPDATE");

    Element elem = (Element) lastVld.item(0);
    String lastV = elem.getTextContent();
    System.out.println(lastV);
} catch (Exception e) {
    e.printStackTrace();
}

I verified I was getting good content by adding a transformer to print out the results to the console.

TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer xform = tFactory.newTransformer();

xform.transform(new DOMSource(doc), new StreamResult(System.out));

There was a couple times I tried running where elem came out null, which I think had something to do with some bad content being retrieved from the URL. This was the content that was printed by the transformer.

<html>
<body>
<script>document.cookie='iiiiiii=11a887d6iiiiiii_11a887d6; path=/';window.location.href=window.location.href;</script>
</body>
</html>

I noticed that if I had this file open in my browser, the code would all of a sudden quit working until I refreshed the page, then it started giving me the right output.

I suspect there's an issue with something at this URL, because when it works properly, this code works fine.

Good luck...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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