2012-06-27 3 views
-4

J'ai créé une application pour analyser un fichier XML à l'aide de SAXParser et le code ci-dessous.java.lang.RuntimeException: Impossible de démarrer l'activité ComponentInfo java.lang.NullPointerException

Voici le code de l'activité principale.

public class XMLAppActivity extends Activity{ 
ArrayList<Question> questionList = new ArrayList<Question>();   
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; 
    } 
} 

Ceci est le code pour mon XMLhandler.

public class QuestionXMLHandler extends DefaultHandler { 

private ArrayList<Question> questionL= new ArrayList<Question>(); 
Question cQuestion; 
Boolean cElement = false; 
String cValue = ""; 


public ArrayList<Question> 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); 
} 
} 

Et ceci est ma classe de questions.

public class Question 
{ 
private String qId; 
private String qName; 
private String qType; 
private String qText; 
private Boolean isRequired; 
private String qSurveyId; 
private List<String> answers; 

public Question() 
{ 
    qId=""; 
    qName=""; 
    qType=""; 
    qText=""; 
    isRequired=false; 
    qSurveyId=""; 

} 
//getters and setter methods 
} 

Mais j'obtiens une erreur de:

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) 
+10

Tu aurais dû mettre moins de code et des bûches !! –

+0

D'où vient l'erreur? Qu'est-ce qui l'a causé? –

+0

mettre logcat complet, et logcat ont le nom de la classe et le numéro de ligne. voir aussi [this] (http://samir-mangroliya.blogspot.in/p/why-nullpointerexception-occures-in.html) –

Répondre

1

Je repéré mon erreur !! c'était dans la méthode End Element de ma classe de gestionnaire XML . Fondamentalement, un problème dans la ligne ci-dessous. if (qName.equalsIgnoreCase ("Réponse")) { cQuestion.getAnswers(). Add (cValue); }

a fonctionné pour moi

1

essayer

xmlIs= this.getResources().getAssets().open("surveyquestions.xml"); 

au lieu de

xmlIs= getAssets().open("surveyquestions.xml"); 
+0

Je peux lire le fichier XML, que j'ai testé, le problème apparaît lorsque j'essaye d'analyser le fichier en utilisant l'analyseur SAX –

+0

Voir @Dheeresh réponse –

+1

J'ai vu @Dheeresh Réponse, mais toujours courir dans le même numéro –

0

Je pense que "questionL" non initialisées dans votre code .........

utilisé dans le code ci-dessous .......

@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); 
      } 

    } 
+0

je l'ai initialisé et mis à jour le code ci-dessus, mais toujours donner la même erreur –

+0

trace de la pile ? –

+0

J'ai ajouté mon chat journal complet !! –

Questions connexes