473,426 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,426 software developers and data experts.

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 1899
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

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

Similar topics

1
by: Andy Dalton | last post by:
Greetings, Does anyone know if there is a tool similar to distcc (http://distcc.samba.org/) for Java? I'm interested in finding a parallel or distributed Java compiler. Thanks for your time....
4
by: Mark A. Washburn | last post by:
/* Test4 WARNING THIS PROGRAM NEVER ENDS DUE TO JAVA COMPILER ERROR Neither Sun JDK 1.1.8 nor JDK 1.4.1 compiler will output *any* error or warning A disassembly of the class file shows...
133
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
25
by: Mohd Hanafiah Abdullah | last post by:
Axiomatic Solutions Sdn Bhd announces the availability of AMPC version 1.2. You can purchase AMPC online now at: http://www.axiomsol.com/hedesu/shopping/index.php Major Changes -------------...
4
by: 'Lil Laurry | last post by:
I have a problem that you may be able with java and since there is no java catergory I have to post it here, when I try to compile java source with the goto function, I try to compile it with...
2
JavaStudent07
by: JavaStudent07 | last post by:
H:\>c:\j2sdk1.4.1\bin\javac.exe BubbleSort.java H:\>c:\j2sdk1.4.1\bin\java.exe BubbleSort Please input the name of the file from which the data will be acquired. Be sure to use the entire path....
4
dmjpro
by: dmjpro | last post by:
does it happen ....???? that if a .class generated in lower version JAVAC and then it going to run by upper version of JVM. could any one tell me the dtls story of these two key terms?? plz help....
0
by: r035198x | last post by:
Inheritance We have already covered one important concept of object-oriented programming, namely encapsulation, in the previous article. These articles are not articles on object oriented...
1
by: RockyIV | last post by:
hey i was jus wondering could anyone tell me how to set up textpad as my java compiler? i want it so that i can compile and run in textpad tools without having to use command line functions. thanks
1
by: mdandreano5 | last post by:
I need help with downloading a compiler for Java. I have tried several times but I can't get anything to download. Can someone please help?
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.