469,317 Members | 1,986 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,317 developers. It's quick & easy.

Why is the java compiler throwing a NullPointerException?

108 100+
That's what I've implemented...


Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import org.xml.sax.*;
  3. import org.xml.sax.helpers.DefaultHandler;
  4. import javax.xml.parsers.SAXParserFactory;
  5. import javax.xml.parsers.ParserConfigurationException;
  6. import javax.xml.parsers.SAXParser;
  7. public class java_xml extends DefaultHandler{
  8.  
  9.  
  10.     public static void main(String argv[])
  11.     {
  12.  
  13.     // Use an instance of ourselves as the SAX event handler
  14.     DefaultHandler handler = new java_xml();
  15.     // Use the default (non-validating) parser
  16.     SAXParserFactory factory = SAXParserFactory.newInstance();
  17.     try {
  18.     // Set up output stream
  19.     OutputStreamWriter out = new OutputStreamWriter(System.out, "UTF8");
  20.     // Parse the input
  21.     SAXParser saxParser = factory.newSAXParser();
  22.     saxParser.parse( "file.xml", handler );
  23.     } catch (SAXException t) {
  24.     System.err.println(t.getLocalizedMessage()+" Errorzz");
  25.     }
  26.     catch (IOException t) {
  27.         System.err.println(t.getLocalizedMessage()+" Errorzz");
  28.         }
  29.     catch (ParserConfigurationException t) {
  30.         t.printStackTrace();
  31.         }
  32.     System.exit(0);
  33.     }
  34.  
  35.  
  36.  
  37.     static private Writer out;
  38.     private void emit(String s)
  39.     throws SAXException
  40.     {
  41.     try {
  42.     out.write(s);
  43.     out.flush();
  44.     } catch (IOException e) {
  45.     throw new SAXException("I/O error", e);
  46.     }
  47.     }
  48.  
  49.  
  50.  
  51.     private void nl()
  52.     throws SAXException
  53.     {
  54.     String lineEnd = System.getProperty("line.separator");
  55.     try {
  56.     //ECHOING AN XML FILE WITH THE SAX PARSER 129
  57.     out.write(lineEnd);
  58.     } catch (IOException e) {
  59.     throw new SAXException("I/O error", e);
  60.     }
  61.     }
  62.  
  63.  
  64.     public void startDocument()
  65.     throws SAXException
  66.     {
  67.     //emit("<?xml version='1.0' encoding='UTF-8'?>");
  68.     //nl();
  69.         System.out.println("parsing starts");
  70.     }
  71.     public void endDocument()
  72.     throws SAXException 
  73.     {
  74.         System.out.println("parsing Ends");
  75.  
  76.  
  77.     }    
  78.  
  79.  
  80.     public void startElement(String namespaceURI,
  81.             String sName, // simple name
  82.             String qName, // qualified name
  83.             Attributes attrs)
  84.             throws SAXException
  85.             {
  86.             String eName = sName; // element name
  87.             if ("".equals(eName)) eName = qName; // not namespace-aware
  88.             emit("<"+eName);
  89.             if (attrs != null) {
  90.             for (int i = 0; i < attrs.getLength(); i++) {
  91.             String aName = attrs.getLocalName(i); // Attr name
  92.             if ("".equals(aName)) aName = attrs.getQName(i);
  93.             emit(" ");
  94.             emit(aName+"=\""+attrs.getValue(i)+"\"");
  95.             }
  96.             }
  97.             emit(">");
  98.             }
  99.             public void endElement(String namespaceURI,
  100.             String sName, // simple name
  101.             String qName // qualified name
  102.             )
  103.             throws SAXException
  104.             {
  105.             //ECHOING AN XML FILE WITH THE SAX PARSER 131
  106.             String eName = sName; // element name
  107.             if ("".equals(eName)) eName = qName; // not namespace-aware
  108.             emit("</"+eName+">");
  109.             }
  110. }

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


Expand|Select|Wrap|Line Numbers
  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <Personnel>
  3. <Employee type="permanent">
  4.         <Name>Seagull</Name>
  5.         <Id>3674</Id>
  6.         <Age>34</Age>
  7.    </Employee>
  8.   <Employee type="contract">
  9.         <Name>Robin</Name>
  10.         <Id>3675</Id>
  11.         <Age>25</Age>
  12.     </Employee>
  13.   <Employee type="permanent">
  14.         <Name>Crow</Name>
  15.         <Id>3676</Id>
  16.         <Age>28</Age>
  17.     </Employee>
  18. </Personnel>


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



Thanks in adv
Aug 27 '10 #1

✓ answered by dbeberman

Where is your
static private Writer out;
being initialized?

4 1710
Frinavale
9,735 Expert Mod 8TB
Do you know which line in the above posted code that the error occurs on?



-Frinny
Aug 27 '10 #2
phpuser123
108 100+
On line 42 and 88
BTW here's the stack trace ..

Exception in thread "main" java.lang.NullPointerException
at java_xml.emit(java_xml.java:42)
at java_xml.startElement(java_xml.java:88)
at com.sun.org.apache.xerces.internal.parsers.Abstrac tSAXParser.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTD Validator.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumen tFragmentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumen tScannerImpl$ContentDispatcher.scanRootElementHook (Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumen tFragmentScannerImpl$FragmentContentDispatcher.dis patch(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumen tFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Co nfiguration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Co nfiguration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLPars er.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.Abstrac tSAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at java_xml.main(java_xml.java:22)
Aug 28 '10 #3
Where is your
static private Writer out;
being initialized?
Aug 28 '10 #4
phpuser123
108 100+
Yes I skipped the initialisaton part..
Thabks, It's working now
Aug 29 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Andy Dalton | last post: by
4 posts views Thread by Mark A. Washburn | last post: by
25 posts views Thread by Mohd Hanafiah Abdullah | last post: by
4 posts views Thread by 'Lil Laurry | last post: by
JavaStudent07
2 posts views Thread by JavaStudent07 | last post: by
dmjpro
4 posts views Thread by dmjpro | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.