Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Using JAVA to parse an XML document

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I am currently working on parsing an XML document such that Java objects could be created and stored in the database. I chose to use SAX since I will only be reading the data, creating the objects and populating the DB.
A NullPointerException,
java.lang.NullPointerException
at org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:658)
at org.apache.crimson.parser.Parser2.parse(Parser2.java:333)
at org.apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:448)
is generated with the following lines of code:
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser parser = spf.newSAXParser();
XMLReader reader = parser.getXMLReader();
reader.setContentHandler(this);
reader.parse(new InputSource("file:///C|/temp/temp.xml"));
Is the error occuring a result of not being able to locate the file? Is there something missing on the classpath (currently using JDK 1.4)?
Thanks in advance for your help!
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Typically, if a file isn't found a more descriptive exception will be thrown. You're going to have to debug this, or at least find out which object is null when it shouldn't be.
It's possible you haven't configured an XML parser like Xerces on your computer, or having included it in your classpath, so your factory in turn returns null when you ask for a SAX parser.
You can step through it in your debugger to find out. Or more likely the stack trace will print out the line where the null pointer exception was thrown.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you're using JDK1.4 (and the stack trace is already coming from org.apache.crimson...), the classpath shouldn't be a problem. My first guess would be that

Originally posted by Tavia Young:
reader.parse(new InputSource("file:///C|/temp/temp.xml"));


has a malformed file path/URL. You should try using a bit different syntax or simply use another constructor for InputSource -- e.g. the one which takes a (File)InputStream.
 
Tavia Young
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much, Nathaniel and Lasse. I installed Xerces and all is well now. However, I will also try Lasse's suggestion. Thanks again! =)
 
I can't take it! You are too smart for me! Here is the tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic