2012-09-17 61 views
0

整個一天,我試圖找出如何讓我的解析器的工作,但由於某種原因,我總是得到一個NullPointerException,這裏是我的代碼:XML SAX解析器不工作 - NullPointerException異常

MainActivity:

public class MainActivity extends Activity { 
    XMLHandler data; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


try { 
      Log.i("PARSER", "versuch!"); 

       /** 
       * Create a new instance of the SAX parser 
       **/ 

       SAXParserFactory saxPF = SAXParserFactory.newInstance(); 
       SAXParser saxP = saxPF.newSAXParser(); 
       XMLReader xmlR = saxP.getXMLReader(); 


       URL url = new URL("my xml file"); // URL of the XML 

       /** 
       * Create the Handler to handle each of the XML tags. 
       **/ 
       data = new XMLHandler(); 
       xmlR.setContentHandler(data); 
       xmlR.parse(new InputSource(url.openStream())); 


      } catch (Exception e) { 
       System.out.println(e); 
       Log.i("PARSER", "ES KLAPPT NICHT!"); 
      } 




     TextView txt1 = (TextView) findViewById(R.id.txtview1); 
     txt1.setText(data.getTournament().get(0).get(0).getTeam1().toString()); 

    } 

XMLHandler:

public class XMLHandler extends DefaultHandler { 

    private Tournament tournament; 
    private TournamentDay currentDay; 
    private Match currentMatch; 
    private StringBuilder builder; 

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

     if (qName.equalsIgnoreCase("team1")) 
      currentMatch.setTeam1(builder.toString()); 
     else if (qName.equalsIgnoreCase("team2")) 
      currentMatch.setTeam2(builder.toString()); 
     else if (qName.equalsIgnoreCase("resultfinal")) 
      currentMatch.setResultfinal(builder.toString()); 
     else if (qName.equalsIgnoreCase("result1")) 
      currentMatch.setResult1(builder.toString()); 
     else if (qName.equalsIgnoreCase("result2")) 
      currentMatch.setResult2(builder.toString()); 
     else if (qName.equalsIgnoreCase("venue")) 
      currentMatch.setVenue(builder.toString()); 
     else if (qName.equalsIgnoreCase("gmt")) 
      currentMatch.setGmt(builder.toString()); 
     else if (qName.equals("match")) 
      currentDay.add(currentMatch); 
     else if (qName.equals("day")) 
      tournament.add(currentDay); 
    } 

    @Override 
    public void startElement(String uri, String localName, String qName, 
          org.xml.sax.Attributes attributes) throws SAXException { 
     if (qName.equals("data")) { 
      tournament = new Tournament(); 
     } 
     if (qName.equals("day")) { 
      currentDay = new TournamentDay(); 
     } 
     else if (qName.equals("match")) { 
      currentMatch = new Match(); 
     } 
     else { 
      builder = new StringBuilder(); 
     } 
    } 

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

    public Tournament getTournament() { 
     return tournament; 
    } 
} 

Tourn柔夷花序:

public class Tournament { 

    private List<TournamentDay> days; 

    public Tournament() { 
     this.days = new ArrayList<TournamentDay>(); 
    } 

    public void add(TournamentDay day) { 
     days.add(day); 
    } 
    public TournamentDay get(int i) { 
     return days.get(i); 
    } 
} 

比賽:

public class Match { 
    private String team1; 
    private String team2; 
    private String resultfinal; 
    private String result1; 
    private String result2; 
    private String date; 
    private String venue; 
    private String gmt; 

    public String getTeam1() { 
     return team1; 
    } 

    public void setTeam1(String team1) { 
     this.team1 = team1; 
    } 

    public String getTeam2() { 
     return team2; 
    } 

    public void setTeam2(String team2) { 
     this.team2 = team2; 
    } 

    public String getResultfinal() { 
     return resultfinal; 
    } 

    public void setResultfinal(String resultfinal) { 
     this.resultfinal = resultfinal; 
    } 

    public String getResult1() { 
     return result1; 
    } 

    public void setResult1(String result1) { 
     this.result1 = result1; 
    } 

    public String getResult2() { 
     return result2; 
    } 

    public void setResult2(String result2) { 
     this.result2 = result2; 
    } 

    public String getDate() { 
     return date; 
    } 

    public void setDate(String date) { 
     this.date = date; 
    } 

    public String getVenue() { 
     return venue; 
    } 

    public void setVenue(String venue) { 
     this.venue = venue; 
    } 

    public String getGmt() { 
     return gmt; 
    } 

    public void setGmt(String gmt) { 
     this.gmt = gmt; 
    } 

} 

最後我的XML文件:

<data> 
    <day> 

     <match> 

      <team1>ind</team1> 
      <team2>afg</team2> 

      <resultfinal></resultfinal> 
      <result1></result1> 
      <result2></result2> 
      <date>19.09</date> 
      <venue>1</venue> 
      <gmt>14:00</gmt> 


     </match> 

     <match> 

      <team1>eng</team1> 
      <team2>afg</team2> 

      <resultfinal></resultfinal> 
      <result1></result1> 
      <result2></result2> 

      <date>21.09</date> 
      <venue>1</venue> 
      <gmt>14:00</gmt> 


     </match> 
     <match> 

      <team1>ind</team1> 
      <team2>eng</team2> 

      <resultfinal></resultfinal> 
      <result1></result1> 
      <result2></result2> 
      <date>23.09</date> 
      <venue>2</venue> 
      <gmt>10:00</gmt> 


     </match> 
    </day> 
    <day> 
     <match> 

      <team1>aust</team1> 
      <team2>irl</team2> 

      <resultfinal></resultfinal> 
      <result1></result1> 
      <result2></result2> 
      <date>19.08</date> 
      <venue>1</venue> 
      <gmt>10:00</gmt> 


     </match> 
     <match> 

      <team1>aust</team1> 
      <team2>west</team2> 

      <resultfinal></resultfinal> 
      <result1></result1> 
      <result2></result2> 
      <date>22.09</date> 
      <venue>1</venue> 
      <gmt>14:00</gmt> 


     </match> 
     <match> 

      <team1>west</team1> 
      <team2>irl</team2> 

      <resultfinal></resultfinal> 
      <result1></result1> 
      <result2></result2> 

      <date>24.09</date> 
      <venue>1</venue> 
      <gmt>14:00</gmt> 


     </match> 

    </day> 

</data> 

這裏是logcat的:

09-17 15:26:49.299: I/System.out(3392): java.lang.NullPointerException 
09-17 15:26:49.299: I/PARSER(3392): ES KLAPPT NICHT! 
09-17 15:26:49.299: D/AndroidRuntime(3392): Shutting down VM 
09-17 15:26:49.299: W/dalvikvm(3392): threadid=1: thread exiting with uncaught exception (group=0xb40a64f0) 
09-17 15:26:49.299: I/Process(3392): Sending signal. PID: 3392 SIG: 9 
09-17 15:26:49.299: D/AndroidRuntime(3392): procName from cmdline: com.example.xmldownloader 
09-17 15:26:49.299: E/AndroidRuntime(3392): in writeCrashedAppName, pkgName :com.example.xmldownloader 
09-17 15:26:49.299: D/AndroidRuntime(3392): file written successfully with content: com.example.xmldownloader StringBuffer : ;com.example.xmldownloader 
09-17 15:26:49.299: E/AndroidRuntime(3392): FATAL EXCEPTION: main 
09-17 15:26:49.299: E/AndroidRuntime(3392): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.xmldownloader/com.example.xmldownloader.MainActivity}: java.lang.NullPointerException 
09-17 15:26:49.299: E/AndroidRuntime(3392):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1698) 
09-17 15:26:49.299: E/AndroidRuntime(3392):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1726) 
09-17 15:26:49.299: E/AndroidRuntime(3392):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
09-17 15:26:49.299: E/AndroidRuntime(3392):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:949) 
09-17 15:26:49.299: E/AndroidRuntime(3392):  at android.os.Handler.dispatchMessage(Handler.java:99) 
09-17 15:26:49.299: E/AndroidRuntime(3392):  at android.os.Looper.loop(Looper.java:130) 
09-17 15:26:49.299: E/AndroidRuntime(3392):  at android.app.ActivityThread.main(ActivityThread.java:3770) 
09-17 15:26:49.299: E/AndroidRuntime(3392):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-17 15:26:49.299: E/AndroidRuntime(3392):  at java.lang.reflect.Method.invoke(Method.java:507) 
09-17 15:26:49.299: E/AndroidRuntime(3392):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880) 
09-17 15:26:49.299: E/AndroidRuntime(3392):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638) 
09-17 15:26:49.299: E/AndroidRuntime(3392):  at dalvik.system.NativeStart.main(Native Method) 
09-17 15:26:49.299: E/AndroidRuntime(3392): Caused by: java.lang.NullPointerException 
09-17 15:26:49.299: E/AndroidRuntime(3392):  at com.example.xmldownloader.MainActivity.onCreate(MainActivity.java:58) 
09-17 15:26:49.299: E/AndroidRuntime(3392):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
09-17 15:26:49.299: E/AndroidRuntime(3392):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1662) 
09-17 15:26:49.299: E/AndroidRuntime(3392):  ... 11 more 

有人可以幫助我嗎?或者指出,我做錯了什麼? 謝謝。

+2

它是什麼代碼:MainActivity.java:58? - 有NullPointerException! – 2012-09-17 19:58:30

+0

這就是:txt1.setText(data.getTournament()。get(0).get(0).getTeam1()。toString()); 這是我想要測試存儲的數據的位置,但是,正如您已經提到的那樣,我收到了該錯誤。但我不知道爲什麼,或者我做錯了什麼 – Ahmad

+0

如果這是第58行,那麼這是你的問題:'txt1.setText(data.getTournament()。get(0).get(0).getTeam1()的ToString());'。它肯定看起來像一個問題:)建議:將你的(許多!)「getXX()」分解成單獨的語句,並修復第一個返回「null」;)然後看看是否有*後續的*「getters」返回null,並修復它們。 – paulsm4

回答

2
txt1.setText(data.getTournament().get(0).get(0).getTeam1().toString()); 

它不是一個代碼,你想看看它! 請將其分成每行一條語句,並記錄下來!

完全一樣,它也是問題here

+0

我把他們分成了單獨的陳述,但現在我意識到他們甚至沒有打電話。問題出在XML處理程序中,但我不知道它有什麼問題 – Ahmad

+0

可能是因爲你想破解xmlR.parse(new InputSource(url.openStream()));聲明也在單獨的聲明中,並檢查輸入流等 – 2012-09-17 21:10:21

+0

啊好吧我現在知道問題出在哪裏。謝謝! – Ahmad