Why is the java compiler throwing a NullPointerException?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phpuser123
    New Member
    • Dec 2009
    • 108

    Why is the java compiler throwing a NullPointerException?

    That's what I've implemented...


    Code:
    import java.io.*;
    import org.xml.sax.*;
    import org.xml.sax.helpers.DefaultHandler;
    import javax.xml.parsers.SAXParserFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.SAXParser;
    public class java_xml extends DefaultHandler{
    
    	
    	public static void main(String argv[])
    	{
    	
    	// Use an instance of ourselves as the SAX event handler
    	DefaultHandler handler = new java_xml();
    	// Use the default (non-validating) parser
    	SAXParserFactory factory = SAXParserFactory.newInstance();
    	try {
    	// Set up output stream
    	OutputStreamWriter out = new OutputStreamWriter(System.out, "UTF8");
    	// Parse the input
    	SAXParser saxParser = factory.newSAXParser();
    	saxParser.parse( "file.xml", handler );
    	} catch (SAXException t) {
    	System.err.println(t.getLocalizedMessage()+" Errorzz");
    	}
    	catch (IOException t) {
    		System.err.println(t.getLocalizedMessage()+" Errorzz");
    		}
    	catch (ParserConfigurationException t) {
    		t.printStackTrace();
    		}
    	System.exit(0);
    	}
    	
    	
    	
    	static private Writer out;
    	private void emit(String s)
    	throws SAXException
    	{
    	try {
    	out.write(s);
    	out.flush();
    	} catch (IOException e) {
    	throw new SAXException("I/O error", e);
    	}
    	}
    	
    	
    
    	private void nl()
    	throws SAXException
    	{
    	String lineEnd = System.getProperty("line.separator");
    	try {
    	//ECHOING AN XML FILE WITH THE SAX PARSER 129
    	out.write(lineEnd);
    	} catch (IOException e) {
    	throw new SAXException("I/O error", e);
    	}
    	}
    	
    	
    	public void startDocument()
    	throws SAXException
    	{
    	//emit("<?xml version='1.0' encoding='UTF-8'?>");
    	//nl();
    		System.out.println("parsing starts");
    	}
    	public void endDocument()
    	throws SAXException 
    	{
    		System.out.println("parsing Ends");
    		
    	
    	}	
    	
    	
    	public void startElement(String namespaceURI,
    			String sName, // simple name
    			String qName, // qualified name
    			Attributes attrs)
    			throws SAXException
    			{
    			String eName = sName; // element name
    			if ("".equals(eName)) eName = qName; // not namespace-aware
    			emit("<"+eName);
    			if (attrs != null) {
    			for (int i = 0; i < attrs.getLength(); i++) {
    			String aName = attrs.getLocalName(i); // Attr name
    			if ("".equals(aName)) aName = attrs.getQName(i);
    			emit(" ");
    			emit(aName+"=\""+attrs.getValue(i)+"\"");
    			}
    			}
    			emit(">");
    			}
    			public void endElement(String namespaceURI,
    			String sName, // simple name
    			String qName // qualified name
    			)
    			throws SAXException
    			{
    			//ECHOING AN XML FILE WITH THE SAX PARSER 131
    			String eName = sName; // element name
    			if ("".equals(eName)) eName = qName; // not namespace-aware
    			emit("</"+eName+">");
    			}
    }

    and my xml file which I name file.xml goes below:


    Code:
    <?xml version='1.0' encoding='UTF-8'?>
    <Personnel>
    <Employee type="permanent">
            <Name>Seagull</Name>
            <Id>3674</Id>
            <Age>34</Age>
       </Employee>
      <Employee type="contract">
            <Name>Robin</Name>
            <Id>3675</Id>
            <Age>25</Age>
        </Employee>
      <Employee type="permanent">
            <Name>Crow</Name>
            <Id>3676</Id>
            <Age>28</Age>
        </Employee>
    </Personnel>


    Why am I getting a NullPointerExce ption?The problem is somewhat in my xml file...



    Thanks in adv
    Last edited by Frinavale; Aug 27 '10, 08:36 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Do you know which line in the above posted code that the error occurs on?



    -Frinny

    Comment

    • phpuser123
      New Member
      • Dec 2009
      • 108

      #3
      On line 42 and 88
      BTW here's the stack trace ..

      Exception in thread "main" java.lang.NullP ointerException
      at java_xml.emit(j ava_xml.java:42 )
      at java_xml.startE lement(java_xml .java:88)
      at com.sun.org.apa che.xerces.inte rnal.parsers.Ab stractSAXParser .startElement(U nknown Source)
      at com.sun.org.apa che.xerces.inte rnal.impl.dtd.X MLDTDValidator. startElement(Un known Source)
      at com.sun.org.apa che.xerces.inte rnal.impl.XMLDo cumentFragmentS cannerImpl.scan StartElement(Un known Source)
      at com.sun.org.apa che.xerces.inte rnal.impl.XMLDo cumentScannerIm pl$ContentDispa tcher.scanRootE lementHook(Unkn own Source)
      at com.sun.org.apa che.xerces.inte rnal.impl.XMLDo cumentFragmentS cannerImpl$Frag mentContentDisp atcher.dispatch (Unknown Source)
      at com.sun.org.apa che.xerces.inte rnal.impl.XMLDo cumentFragmentS cannerImpl.scan Document(Unknow n Source)
      at com.sun.org.apa che.xerces.inte rnal.parsers.XM L11Configuratio n.parse(Unknown Source)
      at com.sun.org.apa che.xerces.inte rnal.parsers.XM L11Configuratio n.parse(Unknown Source)
      at com.sun.org.apa che.xerces.inte rnal.parsers.XM LParser.parse(U nknown Source)
      at com.sun.org.apa che.xerces.inte rnal.parsers.Ab stractSAXParser .parse(Unknown Source)
      at javax.xml.parse rs.SAXParser.pa rse(Unknown Source)
      at javax.xml.parse rs.SAXParser.pa rse(Unknown Source)
      at java_xml.main(j ava_xml.java:22 )

      Comment

      • dbeberman
        New Member
        • Aug 2010
        • 2

        #4
        Where is your
        static private Writer out;
        being initialized?

        Comment

        • phpuser123
          New Member
          • Dec 2009
          • 108

          #5
          Yes I skipped the initialisaton part..
          Thabks, It's working now

          Comment

          Working...