This happens because the field ( DOMParser parser ) in the class is set to
'null'. The main method does NOT instantiate this field. Instead it
creates a second instance of the parser class whose scope is the main
method only. In order to fix this, you should change the first line in the
main method to 'parser = new DOMParser()' instead of 'DOMParser parser =
new DOMParser()'

Ankit Pasricha
XML Parser Development
IBM Toronto Lab
8200 Warden Avenue, Ontario L6G 1C7


                                                                           
             Rob Exley                                                     
             <[EMAIL PROTECTED]                                             
             factory.co.uk>                                             To 
                                       [EMAIL PROTECTED]        
             08/21/2004 04:53                                           cc 
             AM                                                            
                                                                   Subject 
                                       xml schema validation - null        
             Please respond to         pointer                             
             [EMAIL PROTECTED]                                             
                .apache.org                                                
                                                                           
                                                                           
                                                                           
                                                                           




I wouldn't classify myself as a java programmer so be gentle with me....

I am trying to use xerces-j to validate xml documents with an asociated
XML Schema. I have already validated with OxygenXML, xmllint and XSV so
I know all is well. It's just that I would like the option of validating
at run-time also.

I have downloaded some sample code from the IBM tutorial site and
everything seems hunky-dory until I try to enable the schema validation
feature. As soon as the setFeature lines are introduced I start to get
null pointer exceptions being thrown. I would appreciate any help or
guidance that anyone can provide as I am somewhat bemused....

FYI - I am currently trying this against xerces-j 2.6.2 but I seem to
get this error regardless of the version. I have included the source
code as well as the output from running this.

/*
 * Created on Aug 16, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author r_exley
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import java.io.IOException;

public class DOMParserDemo {

        private static void setFeature(String
                feature, boolean setting){

            System.out.println("feature: " + feature);
            System.out.println("setting: " + setting);

                try {
                        parser.setFeature(feature, setting);
                } catch (SAXNotRecognizedException e) {
                        System.out.print("Unrecognized feature: ");
                        System.out.println(feature);
                } catch (SAXNotSupportedException e) {
                        System.out.print("Unrecognized feature: ");
                        System.out.println(feature);
                }
        }

        public static DOMParser parser = null;

    public static void main (String args[]){
        DOMParser parser = new DOMParser();
        Document doc = null;

        setFeature("http://xml.org/sax/features/validation";, true);
             setFeature("http://apache.org/xml/features/validation/schema
",true);
//         try {
//
//parser.parse("../consumer-score/example_score_request.xml");
//             doc = parser.getDocument();
//         } catch (IOException ie){
//            System.out.println("Could not read file.");
//         } catch (SAXException e) {
//            System.out.print("Could not create Document: ");
//            System.out.println(e.getMessage());
//         }

        DOMDisplay.display(doc);
    }
}

[EMAIL PROTECTED]:~/workspace/schema_validation$ echo $JAVA_HOME
/usr/java/j2sdk1.4.2_04
[EMAIL PROTECTED]:~/workspace/schema_validation$ echo $CLASSPATH
/home/r_exley/build/xerces-2_6_2/build/xercesImpl.jar:/home/r_exley/build/xerces-2_6_2/build/xml-apis.jar:.

[EMAIL PROTECTED]:~/workspace/schema_validation$ java DOMParserDemo
feature: http://xml.org/sax/features/validation
setting: true
Exception in thread "main" java.lang.NullPointerException
        at DOMParserDemo.setFeature(DOMParserDemo.java:30)
        at DOMParserDemo.main(DOMParserDemo.java:46)
[EMAIL PROTECTED]:~/workspace/schema_validation$


Thanks in advance

--
Rob Exley <[EMAIL PROTECTED]>
(See attached file: signature.asc)

Attachment: signature.asc
Description: Binary data

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to