使用SAX XML Parser的空指针异常

时间:2021-11-12 00:50:49

I am using the SAX Parser for XML Parsing. The problem is if I print, everything is fine. However, If I want to save anything, I get this error message (with the typos):

我正在使用SAX Parser进行XML解析。问题是如果我打印,一切都很好。但是,如果我想保存任何内容,我会收到此错误消息(使用拼写错误):

"XML Pasing Excpetion = java.lang.NullPointerException" 

My code is given below:

我的代码如下:

Parser code:

try {
        /** Handling XML */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        /** Send URL to parse XML Tags */
        URL sourceUrl = new URL(
        "http://50.19.125.224/Demo/VeryGoodSex_and_the_City_S6E6.xml");

        /** Create handler to handle XML Tags ( extends DefaultHandler ) */
        MyXMLHandler myXMLHandler = new MyXMLHandler();
        xr.setContentHandler((ContentHandler) myXMLHandler);
        xr.parse(new InputSource(sourceUrl.openStream()));

    } catch (Exception e) {
        System.out.println("XML Pasing Excpetion = " + e);
    }

Object to hold XML parsed Info:

用于保存XML解析信息的对象:

public class ParserObject {

String name=null;
String description=null;
String bitly=null; //single
String productLink=null;//single
String productPrice=null;//single
Vector<String> price=null;
}

Handler class:

static ParserObject[] xmlDataObject = null;

public void endElement(String uri, String localName, String qName)
throws SAXException {


    currentElement = false;


    if (qName.equalsIgnoreCase("title"))
    {
        xmlDataObject[index].name=currentValue;
    }

    else if (qName.equalsIgnoreCase("artist"))
    {
        xmlDataObject[index].artist=currentValue;
    } 

}


public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {


    currentElement = true;

    if (qName.equalsIgnoreCase("allinfo"))
    {
        System.out.println("started");
    }

    else if (qName.equalsIgnoreCase("tags"))
    {
        insideTag=1;
    } 

}

public void characters(char[] ch, int start, int length)
throws SAXException {

    if (currentElement) {
        currentValue = new String(ch, start, length);
        currentElement = false;
    }

}

1 个解决方案

#1


0  

Your ParserObject array i.e xmlDataObject is having null value thats is why it is showing null pointer exception. This is my View and it might be wrong but once check it too.

您的ParserObject数组,即xmlDataObject具有空值,这就是它显示空指针异常的原因。这是我的视图,它可能是错误的,但一旦检查它。

#1


0  

Your ParserObject array i.e xmlDataObject is having null value thats is why it is showing null pointer exception. This is my View and it might be wrong but once check it too.

您的ParserObject数组,即xmlDataObject具有空值,这就是它显示空指针异常的原因。这是我的视图,它可能是错误的,但一旦检查它。