Results 1 to 5 of 5
  1. #1
    Rjava is offline Member
    Join Date
    Jul 2008
    Posts
    2
    Rep Power
    0

    Default getting a null pointer exception

    Hi, I am trying to parse a very simple xml file but getting a null pointer exception while reading the text node, for some reason I am getting a null value while reading the text node of an element node, below is the xml and the code snippet ..any help is greatly appreciated..

    <?xml version="1.0" encoding="UTF-8"?>
    <list>
    <fitem localPath="c:\projects\install.log" />
    </list>

    code snippet

    NodeList nodeLst = doc.getElementsByTagName("fitem");
    for (int s = 0; s < nodeLst.getLength(); s++) {

    Node fstNode = nodeLst.item(s);
    if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
    Element fstElmnt = (Element) nodeLst.item(0);
    NodeList fileItm = fstElmnt.getChildNodes();
    System.out.println("FileItem : " + ((Node) fileItm.item(0)).getNodeValue());
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Rjava is offline Member
    Join Date
    Jul 2008
    Posts
    2
    Rep Power
    0

    Default main node..

    If I print the value of the nodelist object nodelst I am getting a null ...

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default

    doc.getElementsByTagName("fitem");
    What does the documentation for the above method say it will return?
    Is null one of the possiblities? If so, your code must check for it.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Basically you have to think about Parent & Child nodes. That means <list> and <fitem> tags. That's the correct way. Start from the parent tag and move ahead.

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 07:48 PM
  2. [SOLVED] Cant figure out null pointer exception
    By todd2230 in forum New To Java
    Replies: 6
    Last Post: 05-06-2008, 08:45 AM
  3. Replies: 2
    Last Post: 05-01-2008, 11:14 AM
  4. Null pointer exception error
    By brownie_jedi in forum New To Java
    Replies: 3
    Last Post: 03-15-2008, 07:27 AM
  5. statement null pointer exception
    By bbq in forum JDBC
    Replies: 1
    Last Post: 07-05-2007, 05:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •