XML SAX Parser NULL指针异常

时间:2014-03-13 02:38:05

标签: java android xml

我正在尝试解析以下XML数据

<questions>
<question id="0">
<text>
Who is the first President of the United States of America?
</text>
<image>
georgewashington.png
</image>
<choices>
<choice answer="true">George Washington</choice>
<choice>Thomas Jefferson</choice>
<choice>James Monroe</choice>
<choice>John Adams</choice> 
</choices>
</question>
    '
    '
    '
</questions>

我尝试使用XML SAX Parser解析它。 这是代码

public class QuestionsParser {
static public class QuestionsSAXParser extends DefaultHandler {
    private ArrayList<Questions> questions;
    private Questions Question = null;
    private Answer answers = null;
    private StringBuilder xmlInnerText;

    public ArrayList<Questions> getquestion() {
        return questions;
    }

    static ArrayList<Questions> parsequestion(InputStream xmlIn)
            throws IOException, SAXException {
        QuestionsSAXParser parser = new QuestionsSAXParser();
        Xml.parse(xmlIn, Xml.Encoding.UTF_8, parser);
        return parser.getquestion();
    }

    @Override
    public void startDocument() throws SAXException {
        xmlInnerText = new StringBuilder();
        questions = new ArrayList<Questions>();

    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        if (localName.equals("question")) {
            Question = new Questions();
            Question.setId(Integer.parseInt(attributes.getValue("id")));
        } else if (localName.equals("choices")) {
            answers = new Answer();
        } else if (localName.equals("choice")) {
            answers.setAnswer(attributes.getValue("answer"));
        }

    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        xmlInnerText.append(ch, start, length);
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        if (localName.equals("image") && Question != null) {
            Question.setImage(xmlInnerText.toString().trim());
        } else if (localName.equals("text")) {
            Question.setText(xmlInnerText.toString().trim());
        } else if (localName.equals("choice") && answers != null) {
            answers.setChoice(xmlInnerText.toString().trim());
            Question.getAnswer().add(answers);
        } else if (localName.equals("choices")) {
            answers = null;
        } else if (localName.equals("question")) {
            questions.add(Question);
            Question = null;
        }
        xmlInnerText.setLength(0);
    }

    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
    }

}

}

public class Questions {
String image, text;
ArrayList<Answer> answer;
int id;
}

public class Answer {
String choice, answer;
}

当我运行它时,应用程序崩溃,而logcat显示它是由NULL指针异常引起的。 我在这里缺少什么?

logcat的:

03-13 03:17:55.906: D/libEGL(2008): loaded /system/lib/egl/libEGL_mali.so
03-13 03:17:55.921: D/libEGL(2008): loaded /system/lib/egl/libGLESv1_CM_mali.so
03-13 03:17:55.926: D/libEGL(2008): loaded /system/lib/egl/libGLESv2_mali.so
03-13 03:17:55.931: E/(2008): Device driver API match
03-13 03:17:55.931: E/(2008): Device driver API version: 23
03-13 03:17:55.931: E/(2008): User space API version: 23
03-13 03:17:55.931: E/(2008): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Fri Nov 29 14:18:37 KST 2013
03-13 03:17:55.956: W/dalvikvm(2008): threadid=11: thread exiting with uncaught exception (group=0x41a23700)
03-13 03:17:55.986: E/AndroidRuntime(2008): FATAL EXCEPTION: AsyncTask #1
03-13 03:17:55.986: E/AndroidRuntime(2008): java.lang.RuntimeException: An error occured while executing doInBackground()
03-13 03:17:55.986: E/AndroidRuntime(2008):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
03-13 03:17:55.986: E/AndroidRuntime(2008):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
03-13 03:17:55.986: E/AndroidRuntime(2008):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
03-13 03:17:55.986: E/AndroidRuntime(2008):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
03-13 03:17:55.986: E/AndroidRuntime(2008):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-13 03:17:55.986: E/AndroidRuntime(2008):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
03-13 03:17:55.986: E/AndroidRuntime(2008):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
03-13 03:17:55.986: E/AndroidRuntime(2008):     at java.lang.Thread.run(Thread.java:841)
03-13 03:17:55.986: E/AndroidRuntime(2008): Caused by: java.lang.NullPointerException
03-13 03:17:55.986: E/AndroidRuntime(2008):     at edu.uncc.itcs5180.midterm.QuestionsParser$QuestionsSAXParser.endElement(QuestionsParser.java:68)
03-13 03:17:55.986: E/AndroidRuntime(2008):     at org.apache.harmony.xml.ExpatParser.endElement(ExpatParser.java:156)
03-13 03:17:55.986: E/AndroidRuntime(2008):     at org.apache.harmony.xml.ExpatParser.appendBytes(Native Method)
03-13 03:17:55.986: E/AndroidRuntime(2008):     at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:513)
03-13 03:17:55.986: E/AndroidRuntime(2008):     at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:474)
03-13 03:17:55.986: E/AndroidRuntime(2008):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:316)
03-13 03:17:55.986: E/AndroidRuntime(2008):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:279)
03-13 03:17:55.986: E/AndroidRuntime(2008):     at android.util.Xml.parse(Xml.java:84)
03-13 03:17:55.986: E/AndroidRuntime(2008):     at edu.uncc.itcs5180.midterm.QuestionsParser$QuestionsSAXParser.parsequestion(QuestionsParser.java:28)
03-13 03:17:55.986: E/AndroidRuntime(2008):     at edu.uncc.itcs5180.midterm.MainActivity$GetTriviaAsync.doInBackground(MainActivity.java:54)
03-13 03:17:55.986: E/AndroidRuntime(2008):     at edu.uncc.itcs5180.midterm.MainActivity$GetTriviaAsync.doInBackground(MainActivity.java:1)
03-13 03:17:55.986: E/AndroidRuntime(2008):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-13 03:17:55.986: E/AndroidRuntime(2008):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
03-13 03:17:55.986: E/AndroidRuntime(2008):     ... 4 more
03-13 03:17:56.061: D/OpenGLRenderer(2008): Enabling debug mode 0

由于

0 个答案:

没有答案