2014-02-28 91 views
-1

數據我有以下錯誤:顯示java.lang.NullPointerException從XML

02-28 15:57:30.866: W/dalvikvm(4610): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 
02-28 15:57:30.896: E/AndroidRuntime(4610): FATAL EXCEPTION: main 
02-28 15:57:30.896: E/AndroidRuntime(4610): java.lang.NullPointerException 

02-28 15:57:30.896: E/AndroidRuntime(4610):  at com.xuexiaozhe.xml.Mp3ListContentHandler.characters(Mp3ListContentHandler.java:51) 
02-28 15:57:30.896: E/AndroidRuntime(4610):  at org.apache.harmony.xml.ExpatParser.text(ExpatParser.java:163) 
02-28 15:57:30.896: E/AndroidRuntime(4610):  at org.apache.harmony.xml.ExpatParser.appendChars(Native Method) 
02-28 15:57:30.896: E/AndroidRuntime(4610):  at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:497) 
02-28 15:57:30.896: E/AndroidRuntime(4610):  at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:484) 
02-28 15:57:30.896: E/AndroidRuntime(4610):  at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:309) 
02-28 15:57:30.896: E/AndroidRuntime(4610):  at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:267) 
02-28 15:57:30.896: E/AndroidRuntime(4610):  at com.xuexiaozhe.list.RemoteMp3ListActivity.parseMp3Info(RemoteMp3ListActivity.java:188) 
02-28 15:57:30.896: E/AndroidRuntime(4610):  at com.xuexiaozhe.list.RemoteMp3ListActivity.access$1(RemoteMp3ListActivity.java:178) 
02-28 15:57:30.896: E/AndroidRuntime(4610):  at com.xuexiaozhe.list.RemoteMp3ListActivity$NetworkHandler.handleMessage(RemoteMp3ListActivity.java:109) 
02-28 15:57:30.896: E/AndroidRuntime(4610):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-28 15:57:30.896: E/AndroidRuntime(4610):  at android.os.Looper.loop(Looper.java:137) 
02-28 15:57:30.896: E/AndroidRuntime(4610):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
02-28 15:57:30.896: E/AndroidRuntime(4610):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-28 15:57:30.896: E/AndroidRuntime(4610):  at java.lang.reflect.Method.invoke(Method.java:511) 
02-28 15:57:30.896: E/AndroidRuntime(4610):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
02-28 15:57:30.896: E/AndroidRuntime(4610):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
02-28 15:57:30.896: E/AndroidRuntime(4610):  at dalvik.system.NativeStart.main(Native Method) 
02-28 15:57:31.005: D/dalvikvm(4610): GC_CONCURRENT freed 407K, 9% free 6016K/6596K, paused 89ms+86ms, total 267ms 

Mp3ListContentHandler類代碼:

package com.xuexiaozhe.xml; 

import java.util.List; 

import org.xml.sax.Attributes; 
import org.xml.sax.SAXException; 
import org.xml.sax.helpers.DefaultHandler; 

import com.xuexiaozhe.model.Mp3Info; 

public class Mp3ListContentHandler extends DefaultHandler { 
    private List<Mp3Info> mp3Infos = null; 
    private Mp3Info mp3Info = null; 
    private String tagName = null; 

    private static final String XML_RESOURCE_TAG = "resource"; 
    private static final String XML_ID_TAG = "id"; 
    private static final String XML_TITLE_TAG = "title"; 
    private static final String XML_ARTIST_TAG = "artist"; 
    private static final String XML_ALBUM_TAG = "album"; 
    private static final String XML_DURATION_TAG = "duration"; 
    private static final String XML_MP3NAME_TAG = "mp3Name"; 
    private static final String XML_MP3SIZE_TAG = "mp3Size"; 
    private static final String XML_LRCNAME_TAG = "lrcName"; 
    private static final String XML_LRCSIZE_TAG = "lrcSize"; 
    private static final String XML_BACKGROUND_TAG = "background"; 

    public Mp3ListContentHandler(List<Mp3Info> mp3Infos) { 
     super(); 
     this.mp3Infos = mp3Infos; 
    } 

    public List<Mp3Info> getMp3Infos() { 

     return mp3Infos; 

    } 

    public void setMp3Infos(List<Mp3Info> mp3Infos) { 
     this.mp3Infos = mp3Infos; 
    } 

    @Override 
    public void characters(char[] ch, int start, int length) 
      throws SAXException { 
     // ¸ّmp3Infoةèضأµؤب«تاخؤ¼‏أû£¬£¬¸ù¾فxmlخؤ¼‏ض±½سةèضأ£¬´ّخؤ¼‏أû؛َ×؛ 
     String temp = new String(ch, start, length); 
     if (XML_ID_TAG.equals(tagName)) { 
      mp3Info.setId(Integer.parseInt(temp)); 
     } else if (XML_TITLE_TAG.equals(tagName)) { 
      mp3Info.setTitle(temp); 
     } else if (XML_ARTIST_TAG.equals(tagName)) { 
      mp3Info.setArtist(temp); 
     } else if (XML_ALBUM_TAG.equals(tagName)) { 
      mp3Info.setAlbum(temp); 
     } else if (XML_DURATION_TAG.equals(tagName)) { 
      mp3Info.setDuration(Long.parseLong(temp)); 
     } else if (XML_MP3NAME_TAG.equals(tagName)) { 
      mp3Info.setMp3Name(temp); 
     } else if (XML_MP3SIZE_TAG.equals(tagName)) { 
      mp3Info.setMp3Size(Long.parseLong(temp)); 
     } else if (XML_LRCNAME_TAG.equals(tagName)) { 
      mp3Info.setLrcName(temp); 
     } else if (XML_LRCSIZE_TAG.equals(tagName)) { 
      mp3Info.setLrcSize(Long.parseLong(temp)); 
     } else if (XML_BACKGROUND_TAG.equals(tagName)) { 
      mp3Info.setBackgroundName(temp); 
     } 
    } 

    @Override 
    public void endDocument() throws SAXException { 

    } 

    @Override 
    public void endElement(String uri, String localName, String qName) 
      throws SAXException { 
     if (XML_RESOURCE_TAG.equals(qName)) { 
      mp3Infos.add(mp3Info); 
     } 
     tagName = ""; 
    } 

    @Override 
    public void startDocument() throws SAXException { 

    } 



     @Override 
    public void startElement(String uri, String localName, String qName, 
      Attributes attributes) throws SAXException { 
     super.startElement(uri, localName, qName, attributes); 

     tagName = localName; 
     if (tagName.equals("resource")) { 
      mp3Info = new Mp3Info(); 
     } 
    } 

    public Mp3Info getMp3Info() { 
     return mp3Info; 
    } 

    public void setMp3Info(Mp3Info mp3Info) { 
     this.mp3Info = mp3Info; 
    } 

    public List<Mp3Info> getmp3Infos() { 
     return mp3Infos; 
    } 

    public void setMp3List(List<Mp3Info> mp3Infos) { 
     this.mp3Infos = mp3Infos; 
    } 

} 

我怎樣才能解決這個問題?

+0

什麼是第51行?有NPE。 –

+0

請發佈您的'Mp3ListContentHandler'類。 – Manitoba

+0

我修改後的帖子 – user3245604

回答

0

mp3Info變量未初始化,它設置爲null

事實上,在第13行:

private Mp3Info mp3Info = null; 

應該在方法startElement被初始化,但我不知道何時何地調用此方法。

+0

private String tagName; 同樣的問題:* _ *: – user3245604

+0

@ user3245604是的,這個變量是一樣的。它應該在'startElement'方法中初始化。 –

+0

可以請你爲我做^ _^ – user3245604

0

似乎你的mp3Info類字段爲空。所以,當我從代碼中看到您張貼到設置mp3Info唯一的辦法就是在這裏

if (XML_RESOURCE_TAG.equals(tagName)) { mp3Info = new Mp3Info(); }

這樣你就可以在你的人物的方法之前檢查mp3Info爲空if語句: 像

if (mp3Info == null) { 
    mp3Info = new Mp3Info(); 
} 

而我看到的另一個原因是隻有方法startElement永遠不會獲得localName的值「資源」

相關問題