Addy
Addy

Reputation: 427

JDom Null Pointer Exception

I am trying to read through XML

my XML file is

 <datasource caption='Benfords' inline='true' name='oracle.41859.392947812499' version='8.2'>
      <connection authentication='RDBMS' class='oracle' port='1531' **server**='honeyWillServer'  service ="de06" username='IIUSER'>
        <relation name='TableauSQL' type='text'>
</Connection>
</datasource>    

my source code:

public class XMLReader {
    public static void main(String[] args) {         
      try {         
        SAXBuilder builder = new SAXBuilder();
        File xmlFile = new File("C:\\Users\\c200433\\Desktop\\RBM - Law.twb"); // XML file
        Document doc = (Document) builder.build(xmlFile);
        Element rootNode = doc.getRootElement();
 Element staff = rootNode.getChild("datasource");
            Element staff1 = rootNode.getChild("connection");
            staff1.getAttribute("server").setValue("aventador.am.lilly.com:1530/tst806");             
        System.out.println("File updated!");
      } 
            catch (Exception e) {
        e.printStackTrace();
                           }
    }
}

Task is simple i want to drill through xml and replace server (in connection node ) using setValue to desired.
I am getting a java.lang.NullPointerException.

Upvotes: 0

Views: 595

Answers (1)

BatScream
BatScream

Reputation: 19700

First ,

Your XML is not well formed:

<connection> </Connection> --? capital 'C' in end tag

<relation name='TableauSQL' type='text'> -- Where is the end tag for relation.?

After fixing those try the below,

Element staff1 = staff.getChild("connection"); // Connection is the child of datasource.

instead of:

Element staff1 = rootNode.getChild("connection");

Upvotes: 1

Related Questions