| Author |
root.getRootElement() is throwing an NullPointerException
|
K Sathya Narayanan
Greenhorn
Joined: Feb 15, 2007
Posts: 28
|
|
hi 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; try { 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
Greenhorn
Joined: Feb 15, 2007
Posts: 28
|
|
hi i have found out the solution Steps 1.Download the xercesImpl.jar file from the below link http://www.java2s.com/Code/Jar/XML/DownloadxercesImpljar.htm there are lot such jar file in the below website http://www.java2s.com/Code/Jar/CatalogJar.htm 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 located 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
Greenhorn
Joined: Feb 15, 2007
Posts: 28
|
|
hi if(root!=null) { context.put("root", root.getRootElement()); System.out.println("Value ="+ root.getRootElement().getChild("document").getChild("properties") .getChild("title").getText()); } /*root.getRootElement().getChild("document").getChild("properties") .getChild("title").getText() is throwing the NullPointer Exception*/ when i use the above line in my java class it is throwing the below exception java.lang.NullPointerException [ January 08, 2008: Message edited by: K Sathya Narayanan ]
|
 |
K Sathya Narayanan
Greenhorn
Joined: Feb 15, 2007
Posts: 28
|
|
hi 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 " + "webapps\\ROOT\\SampleVelocityProject\\WebContent WEB-INF"; root = builder.build(path+" test.xml"); System.out.println("Value = #"+ root.getRootElement().getChild("properties").getChild("title").getText()); context.put("root", root); In Template file , i displayed using the below code. $root.getRootElement().getChild("properties").getChild("title").getText()
|
 |
 |
|
|
subject: root.getRootElement() is throwing an NullPointerException
|
|
|