SAX error with Sun tutorial
Hello,
I'm looking at the different ways to parse XML files using Java and currently I'm stuck with SAX. I'm following the JAXP tutorial here at the Sun site and I can't manage to run the echo example: http://java.sun.com/xml/jaxp-1.1/docs/tutorial/sax/2a_echo.html
Several posts here pointed out it may be the version of Crimson that was buggy but after downloading version 1.1.1 I still get the same error as before. I also put all the jar files in the rigth order in the classpath (crimson.jar then jaxp.jar) and I cleared the lib/ext directory. I get the same error using JBuilder or running from command line.
Then I decided to give it a go with the Xerces parser. I transformed the main method to (all the rest stayed the same as in the example in the tutorial):
DefaultHandler handler = new SAXEcho();
try {
SAXParser saxParser = new SAXParser();
saxParser.setContentHandler(handler);
saxParser.parse(new InputSource(new FileReader(args[0])));
// Set up output stream
out = new OutputStreamWriter(System.out, "UTF8");
} catch (Throwable t) {
t.printStackTrace();
}
SAXEcho extends the DefaultHandler class and implements methods like startDocument, startElement, etc.
The error I'm getting is:
java.lang.NullPointerException at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:917) at xmlparsing.SAXEcho.main(SAXEcho.java:103)
Looks like the error is actually thrown by a class file in Xerces package which seems very strange since Crimson also gave me the same type of error.
Any help on this would be greatly appreciated since I spent already quite some time on this.
Kind regards,
Hans.
Why don't you check at which point exactly you get that exception?
For example, it might be the absence of args[0].
The fact that "null pointer " exception is thrown in one particular class means really not much.
Also, why don't you use SAXParserFactory?
Do you have any other working examples?
I get the exception when calling the parse method. The args[0] is the xml file to be parsed. The error occurs when it finds the file.
I worked with DOM before and that worked. Isn't the SAXParserFactory a pattern in the Crimson parser? I try the Xerces parser here to make sure it wasn't the Crimson parser that caused the problem.
Is the code not correct?
> Why don't you check at which point exactly you get
> that exception?
> For example, it might be the absence of args[0].
> The fact that "null pointer " exception is thrown in
> one particular class means really not much.
>
> Also, why don't you use SAXParserFactory?
> Do you have any other working examples?
I get the exception when calling the parse method. The args[0] is the xml file to be parsed. The error occurs when it finds the file.
I worked with DOM before and that worked. Isn't the SAXParserFactory a pattern in the Crimson parser? I try the Xerces parser here to make sure it wasn't the Crimson parser that caused the problem.
Is the code not correct?
> Why don't you check at which point exactly you get
> that exception?
> For example, it might be the absence of args[0].
> The fact that "null pointer " exception is thrown in
> one particular class means really not much.
>
> Also, why don't you use SAXParserFactory?
> Do you have any other working examples?
Can you post your exact classpath, jdk version ?I just tried Echo01 and it compiles and runs and parses just fine, regardless if I use Crimson-1.1, Crimson-1.1.1 or Xerces.
lk555 at 2007-6-29 11:43:30 >

I did not run Xerces and frankly did not try any their examples.
All I can tell that it runs fine with the XML I create and with usage of factory.
I'm not sure what happens down there.
The reason I replied at all is the Null Pointer exception.
I find myself getting into similar situations at times. Usually the cause of the error lays somewhere where you expect it less.
I'll try to see if I can get something out of this pesky Echo example :)
Regards,
Nikolay.
Thanks for replying.
My commandline command:
d:\JDK1.3.1\bin\java.exe -cp .;d:\JBuilder4\lib\ext\xerces.jar;d:\jaxp-1.1\jaxp.jar xmlparsing.SAXEcho sample.xml
Returning:
java.lang.NullPointerException at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:917) at xmlparsing.SAXEcho.main(SAXEcho.java:103)
Regards,
Hans.
Well, I found it. Turns out I was looking in the wrong place. Had nothing do to with the parsing itself really. The code below is wrong, obviously my OutPutStream should be set before I start parsing. Line 7> should be put before line 3>.
Sorry to have bothered you with such a dumb mistake. Thanks for trying to help me guys.
That should teach me not to just cut & paste, huh? ;-)
Kind regards,
Hans.
1> try {
2>SAXParser saxParser = new SAXParser();
3>saxParser.setContentHandler(handler);
4>saxParser.parse(new InputSource(new FileReader(args[0])));
5> // Set up output stream
6> out = new OutputStreamWriter(System.out, "UTF8");
7>} catch (Throwable t) {
8> t.printStackTrace();
9>}