wood burning stoves
The moose likes Other Application Frameworks and the fly likes root.getRootElement() is throwing an NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "root.getRootElement() is throwing an NullPointerException" Watch "root.getRootElement() is throwing an NullPointerException" New topic

root.getRootElement() is throwing an NullPointerException

K Sathya Narayanan

Joined: Feb 15, 2007
Posts: 28
i want to read a xml file and store it in a String Variable.i have placed test.xml in WEB-INF and Webcontent.

SAXBuilder builder;
Document root = null;

builder = new SAXBuilder(
"org.apache.xerces.parsers.SAXParser" );

//i think test.xml is not found
root = builder.build("test.xml");
catch( Exception ee)

The below code is throwing an Nullpointer exception
,since root has the value null.
context.put("root", root.getRootElement());

i guess the test.xml is not found in the above statement.
please tell me where to place the test.xml file or what changes i should make to make the above program work

Thanks in Advance
K Sathya Narayanan

Joined: Feb 15, 2007
Posts: 28
i have found out the solution

1.Download the xercesImpl.jar file from the below link

there are lot such jar file in the below website

2.Add xercesImpl.jar file in your WEB-INF/lib folder

This will prevent the ClasscastException

Next you will get the ClassNotFoundException is removed , but , i now get the below exception
java.io.FileNotFoundException: D:\Eclipse3.2\eclipse\test.xml (The system cannot find the file specified)

The SAXBuilder is looking for test.xml in D:\Eclipse3.2\eclipse

so for time-being , you have to spcify the exact path in which the xml file is

for example if test.xml is in d:\projects\DataProjects

use the below code

builder = new SAXBuilder( "org.apache.xerces.parsers.SAXParser" );

String path="D:\\projects\\DataProjects\\";

root = builder.build(path+"\\test.xml");

Then the fileNotException will be eliminated
K Sathya Narayanan

Joined: Feb 15, 2007
Posts: 28

context.put("root", root.getRootElement());

System.out.println("Value ="+ root.getRootElement().getChild("document").getChild("properties")


is throwing the NullPointer Exception*/

when i use the above line in my java class it is throwing the below exception

[ January 08, 2008: Message edited by: K Sathya Narayanan ]
K Sathya Narayanan

Joined: Feb 15, 2007
Posts: 28
i have found the solution

Thanks a Lot ,
i changed as below in java Code

builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser" );

String path="D:
Program Files
Apache Software Foundation
Tomcat 5.5
" +

root = builder.build(path+"

System.out.println("Value = #"+ root.getRootElement().getChild("properties").getChild("title").getText());

context.put("root", root);

In Template file , i displayed using the below code.

I agree. Here's the link: http://aspose.com/file-tools
subject: root.getRootElement() is throwing an NullPointerException
It's not a secret anymore!