开发者

null pointer exception using SAX XML Parser

开发者 https://www.devze.com 2023-04-01 23:08 出处:网络
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):

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):

"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:

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

}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消