2011-09-03 2 views
0

XML 구문 분석 용 SAX 구문 분석기를 사용하고 있습니다. 문제는 인쇄하면 모든 것이 정상입니다. 개최SAX XML 구문 분석기를 사용하는 null 포인터 예외

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

대상 :

파서 코드 :

"XML Pasing Excpetion = java.lang.NullPointerException" 

이 내 코드는 아래와 같습니다 : 난 아무것도 저장하려면 그러나, 나는 (오타 포함)이 오류 메시지가

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

핸들러 클래스 :

XML은 정보 분석
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

스택 추적을 게시 할 수 있습니까? catch 블록에'e.printStackTrace();'를 추가하면 문제가있는 곳을 정확하게 볼 수 있습니다. –

+0

제안에 감사드립니다 ... 문제가 해결되었습니다. – user926293

답변

0

귀하의 ParserObject 배열, 즉 xmlDataObject에 null 값이있는 이유는 이것이 null 포인터 예외를 표시하는 이유입니다. 이것은 내 견해이며 틀릴 수도 있지만 한번 확인해보십시오.

+0

고맙습니다. – user926293

관련 문제