0

Hello I am working on the google NewsReader Application Using SAX, Here I am using getters and setters methods for get the data.
But I have issue that I could add the value into the setters methods but could not get into the getters methods, I don't know what's Mistake, Please let me know where I do Mistake .

public class Item implements Serializable {

private String title;
private String description;
private String link;
private String date;

public Item() {
    setTitle(null);
    setDescription(null);
    setLink(null);
    setDate(null);
}

public String getTitle() {
    System.out.println(" value of the Gettitle ===============> "+ title);
    return title;
}

public void setTitle(String title) {
    this.title = title;
    System.out.println("value of the settitle =============>"+this.title);
}

Output into Logcat:

 value of the settitle =============>Oracle CEO Ellison to Buy Most of Hawaiian Island Lanai - Bloomberg

 value of the Gettitle ===============> null

Here I got the value of item.getTitle().length() is null.

Edit:

public void endElement(String uri, String localName, String qName)
        throws SAXException {
    // TODO Auto-generated method stub
    //super.endElement(uri, localName, qName);

    if(localName.equalsIgnoreCase("title")) {

        if(inItem) {
            //System.out.println("value of the content=========>"+content.toString());
            item.setTitle(content.toString());
        } else {
            channel.setTitle(content.toString());
        }
    }

 Item item  = new Item();



        for (int i = 0; i < item.getTitle().length(); i++) {

            HashMap<String, String> map = new HashMap<String, String>();

            map.put("title", item.getTitle().valueOf(i));
            map.put("pubdate", item.getDate().valueOf(i));
            map.put("desc", item.getDescription().valueOf(i));

             items.add(map);

        }
4

3 回答 3

1

我强烈怀疑您调用getTitle()的实例与Item您调用的实例不同setTitle()

Item item1 = new Item();
item1.setTitle("foo");

// Some other code

Item item2 = new Item();
String title = item2.getTitle(); // This will return null
于 2012-06-21T06:45:57.800 回答
0

如果您对 和 使用 2 个不同的对象,则可能会发生这种set情况get。检查您是否在同一个对象上调用setterand 。getter

编辑

用于设置标题的对象 ieitem.setTitle(content.toString());与在循环item之前创建的对象不同。您正在通过在循环之前使用来for重新创建/重新初始化item对象。创建一个全局对象并只创建一次。Item item = new Item()foritem

于 2012-06-21T06:47:37.567 回答
0

尝试这个,

public class Item implements Serializable {

private String title;
private String description;
private String link;
private String date;

public Item() {
    setTitle(null);
    setDescription(null);
    setLink(null);
    setDate(null);
}

public String getTitle() {
    System.out.println(" value of the Gettitle ===============> "+ title);
    return title;
}

public void setTitle(String title) {
    this.title = title;
    System.out.println("value of the settitle =============>"+this.title);
}

public static void main(String[] args) {

 Item i = new Item();
 i.setTitle("War-Craft");

  i.getTitle();
}
}

请尝试检查您正在设置和获取的实例。

于 2012-06-21T06:52:19.577 回答