Note that there are some explanatory texts on larger screens.

plurals
  1. POjava.lang.RuntimeException: Unable to start activity ComponentInfo java.lang.NullPointerException
    text
    copied!<p>I created an app to parse a XML file using SAXParser and the code is below.</p> <p>Below is the code of my main activity.</p> <pre><code>public class XMLAppActivity extends Activity{ ArrayList&lt;Question&gt; questionList = new ArrayList&lt;Question&gt;(); TextView tvXmlReader; InputSource xmlSource; InputStream xmlIs; String xmlFile; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tvXmlReader=(TextView)findViewById(R.id.my_xml); try { xmlIs= getAssets().open("surveyquestions.xml"); xmlFile = convertStreamToString(xmlIs); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } xmlSource= new InputSource(new StringReader(xmlFile)); tvXmlReader.setText(parseXML().toString()); } private String parseXML() { try { /** Handling XML */ SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); QuestionXMLHandler myXMLHandler = new QuestionXMLHandler(); xr.setContentHandler(myXMLHandler); xr.parse(xmlSource); questionList= myXMLHandler.getQuestionList(); return "Success"; } catch (Exception e) { return "Failure due to " + e.toString(); } } public String convertStreamToString(InputStream is) throws IOException { Writer writer = new StringWriter(); char[] buffer = new char[2048]; try { Reader reader = new BufferedReader(new InputStreamReader(is,"UTF-8")); int n; while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } } finally { is.close(); } String text = writer.toString(); return text; } } </code></pre> <p>This is the code for my XMLhandler.</p> <pre><code>public class QuestionXMLHandler extends DefaultHandler { private ArrayList&lt;Question&gt; questionL= new ArrayList&lt;Question&gt;(); Question cQuestion; Boolean cElement = false; String cValue = ""; public ArrayList&lt;Question&gt; getQuestionList() { return questionL; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { cElement = true; cValue = ""; // if current element is Question , create new question // clear tmpValue on start of element if (qName.equalsIgnoreCase("Question")) { cQuestion = new Question(); cQuestion.setQuestionId(attributes.getValue("id")); cQuestion.setQuestionName(attributes.getValue("name")); cQuestion.setQuestionType(attributes.getValue("type")); cQuestion.setQuestionIsRequired(Boolean.parseBoolean(attributes.getValue("isRequired"))); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (qName.equalsIgnoreCase("Question")) { questionL.add(cQuestion); } if (qName.equalsIgnoreCase("QuestionText")) { cQuestion.setQuestionText(cValue); } if(qName.equalsIgnoreCase("Answer")){ cQuestion.getAnswers().add(cValue); } } @Override public void characters(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub cValue = new String(ch,start,length); } } </code></pre> <p>And This is my Question class.</p> <pre><code>public class Question { private String qId; private String qName; private String qType; private String qText; private Boolean isRequired; private String qSurveyId; private List&lt;String&gt; answers; public Question() { qId=""; qName=""; qType=""; qText=""; isRequired=false; qSurveyId=""; } //getters and setter methods } </code></pre> <p>But I m getting an error of:</p> <pre><code>06-27 14:44:36.148: W/System.err(591): java.lang.NullPointerException 06-27 14:44:36.187: W/System.err(591): at com.optimus.mobile.xml.QuestionXMLHandler.endElement(QuestionXMLHandler.java:52) 06-27 14:44:36.187: W/System.err(591): at org.apache.harmony.xml.ExpatParser.endElement(ExpatParser.java:160) 06-27 14:44:36.187: W/System.err(591): at org.apache.harmony.xml.ExpatParser.append(Native Method) 06-27 14:44:36.187: W/System.err(591): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:505) 06-27 14:44:36.197: W/System.err(591): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:492) 06-27 14:44:36.197: W/System.err(591): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:308) 06-27 14:44:36.197: W/System.err(591): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:264) 06-27 14:44:36.197: W/System.err(591): at com.optimus.mobile.xml.XMLAppActivity.parseXML(XMLAppActivity.java:68) 06-27 14:44:36.197: W/System.err(591): at com.optimus.mobile.xml.XMLAppActivity.onCreate(XMLAppActivity.java:52) 06-27 14:44:36.197: W/System.err(591): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 06-27 14:44:36.197: W/System.err(591): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 06-27 14:44:36.197: W/System.err(591): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 06-27 14:44:36.197: W/System.err(591): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 06-27 14:44:36.197: W/System.err(591): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 06-27 14:44:36.197: W/System.err(591): at android.os.Handler.dispatchMessage(Handler.java:99) 06-27 14:44:36.207: W/System.err(591): at android.os.Looper.loop(Looper.java:123) 06-27 14:44:36.207: W/System.err(591): at android.app.ActivityThread.main(ActivityThread.java:4627) 06-27 14:44:36.207: W/System.err(591): at java.lang.reflect.Method.invokeNative(Native Method) 06-27 14:44:36.207: W/System.err(591): at java.lang.reflect.Method.invoke(Method.java:521) 06-27 14:44:36.207: W/System.err(591): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 06-27 14:44:36.207: W/System.err(591): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 06-27 14:44:36.207: W/System.err(591): at dalvik.system.NativeStart.main(Native Method) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload