0

Im trying to extract data from a database using xml tags but I keeping getting this error

'java.lang.String org.w3c.dom.Node.getNodeValue()' on a null object reference

Not sure why its saying the tag is null when I have a log of the string before I try to extracts the data and it looks like it should I believe.

<?xml version="1.0" ?>
  <database>
    <account>
     <id>1</id>
     <fname>john</fname>
     <lname>smith</lname>
     <status>1</status>
   </account>
 </database>

That is the output from the string which I'm trying to extract the data from and here is the code I'm using to extract it.

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();;
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document dom = db.parse (new ByteArrayInputStream(s.getBytes()));
        Element docEle = dom.getDocumentElement ();

        NodeList nl = docEle.getElementsByTagName("account");

        Element entry = (Element)nl.item(0);
        Element id = (Element)entry.getElementsByTagName("id").item(0);
        Element fname= (Element)entry.getElementsByTagName("fname").item(0);
        Element lname = (Element)entry.getElementsByTagName("lname").item(0);
        Element status= (Element)entry.getElementsByTagName("status").item(0);

        user_id = id.getFirstChild().getNodeValue();
        user_fname = fname.getFirstChild().getNodeValue();
        user_lname = lname.getFirstChild().getNodeValue();
        user_status = status.getFirstChild().getNodeValue();

The app crashes when it try's to get the node value from the first element, any help Is appreciated and thanks in advance

Andrew Dean
  • 195
  • 1
  • 7
  • 23
  • The code above is very likely subtly wrong. If the default `file.encoding` is not UTF-8 (on Windows, it's Cp1252), `s.getBytes()` will return an array of Windows-1252 data, but since the XML doesn't declare an encoding it will default to UTF-8. Any characters above code point 127 will either be garbled, or the parse will fail. Never call the overload of `getBytes()` that doesn't take a `Charset`. Use `s.getBytes(StandardCharsets.UTF_8)`. – David Conrad Feb 12 '17 at 14:26
  • Hi, I added the code you suggested but the app is still crashing, the server I am connecting to linux based if that makes a difference. – Andrew Dean Feb 12 '17 at 14:33
  • 1
    Which one comes up `null`? Possible duplicate of [What is a NullPointerException and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – David Conrad Feb 12 '17 at 14:42
  • You should learn [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). It is much more effective than asking here. – Thomas Fritsch Feb 12 '17 at 15:08

2 Answers2

0

You have started the <account> tag but did not end it. Change </user> to </account>

Rishav Sengupta
  • 241
  • 2
  • 4
0

Managed to figure it out, and this would be helpful for anyone with similar issues i hope.

to be able to read the tag names from an xml string I did the following (s is the xml string variable)

            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(s));

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();;
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document dom = db.parse(is);
            Element docEle = dom.getDocumentElement ();

            NodeList nl = docEle.getChildNodes();

            if (nl != null) {
                int length = nl.getLength();
                for (int i = 0; i < length; i++) {
                    if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
                        Element el = (Element) nl.item(i);
                        if (el.getNodeName().contains("account")) {
                            user_id = el.getElementsByTagName("id").item(0).getTextContent();
                            user_fname = el.getElementsByTagName("fname").item(0).getTextContent();
                            user_lname = el.getElementsByTagName("lname").item(0).getTextContent();
                            user_status = el.getElementsByTagName("status").item(0).getTextContent();
                        }
                    }
                }
            }

the user_ variables have the xml tag values

Andrew Dean
  • 195
  • 1
  • 7
  • 23