2011-08-09 9 views
-3

このリンクからパーサー用にこのコードを取得しました。ヌルポインターエラーを修正するのに役立つボディー

http://www.warriorpoint.com/blog/2009/07/19/android-simplified-source-code-for-parsing-and-working-with-xml-data-and-web-services-in-android/

すべてのボディは、私はこの問題を解決する手助けすることができます私はjava.lang.NullPointerExceptionが

java.util.Calendar.setTime(Calendar.java:1284) 

java.text.SimpleDateFormat.formatImpl(SimpleDateFormat.java:730) 

java.text.SimpleDateFormat.format(SimpleDateFormat.java:1011) 

java.text.DateFormat.format(DateFormat.java:384) 

org.example.reader.Message.getDate(Message.java:46) .... Line 46 is return FORMATTER.format(this.date); 

org.example.reader.Message.toString(Message.java:77)... Line 77 is sb.append(this.getDate()); 

java.lang.StringBuilder.append(StringBuilder.java:203) 

を取得しています...ありがとう...

import java.net.MalformedURLException; 
import java.net.URL; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 

public class Message implements Comparable<Message>{ 
    static SimpleDateFormat FORMATTER = 
     new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z"); 
    private String title; 
    private URL link; 
    private String description; 
    private Date date; 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title.trim(); 
    } 
    // getters and setters omitted for brevity 
    public URL getLink() { 
     return link; 
    } 

    public void setLink(String link) { 
     try { 
      this.link = new URL(link); 
     } catch (MalformedURLException e) { 
      throw new RuntimeException(e); 
     } 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description.trim(); 
    } 

    public String getDate() { 
     return FORMATTER.format(this.date); 
    } 

    public void setDate(String date) { 
     // pad the date if necessary 
     while (!date.endsWith("00")){ 
      date += "0"; 
     } 
     try { 
      this.date = FORMATTER.parse(date.trim()); 
     } catch (ParseException e) { 
      throw new RuntimeException(e); 
     } 
    } 

    public Message copy(){ 
     Message copy = new Message(); 
     copy.title = title; 
     copy.link = link; 
     copy.description = description; 
     copy.date = date; 
     return copy; 
    } 

    @Override 
    public String toString() { 
     StringBuilder sb = new StringBuilder(); 
     sb.append("Title: "); 
     sb.append(title); 
     sb.append('\n'); 
     sb.append("Date: "); 
     sb.append(this.getDate()); 
     sb.append('\n'); 
     sb.append("Link: "); 
     sb.append(link); 
     sb.append('\n'); 
     sb.append("Description: "); 
     sb.append(description); 
     return sb.toString(); 
    } 

    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((date == null) ? 0 : date.hashCode()); 
     result = prime * result 
       + ((description == null) ? 0 : description.hashCode()); 
     result = prime * result + ((link == null) ? 0 : link.hashCode()); 
     result = prime * result + ((title == null) ? 0 : title.hashCode()); 
     return result; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     Message other = (Message) obj; 
     if (date == null) { 
      if (other.date != null) 
       return false; 
     } else if (!date.equals(other.date)) 
      return false; 
     if (description == null) { 
      if (other.description != null) 
       return false; 
     } else if (!description.equals(other.description)) 
      return false; 
     if (link == null) { 
      if (other.link != null) 
       return false; 
     } else if (!link.equals(other.link)) 
      return false; 
     if (title == null) { 
      if (other.title != null) 
       return false; 
     } else if (!title.equals(other.title)) 
      return false; 
     return true; 
    } 

    public int compareTo(Message another) { 
     if (another == null) return 1; 
     // sort descending, most recent first 
     return another.date.compareTo(date); 
    } 
} 
+0

どのクラスがこれらのメソッドを呼び出していますか?すべてのセッターが呼び出されていることを確認してください。 – ngesh

+0

'date'フィールドは初期化されていませんか? – dhblah

答えて

0

はあなたの日付を初期化してみ。 dateの値がnullであるため、NULLポインタ例外が発生しています。

関連する問題