【问题标题】:Issue with getters and setters Methodsgetter 和 setter 方法的问题
【发布时间】:2012-06-23 09:00:40
【问题描述】:

您好,我正在使用SAX 开发google NewsReader 应用程序,这里我使用getterssetters 方法来获取数据。
但是我有一个问题,我可以将值添加到 setters 方法中,但无法进入 getters 方法,我不知道什么是错误,请让我知道我在哪里犯了错误。

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

输出到 Logcat:

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

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

这里我得到item.getTitle().length() 的值为空。

编辑:

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

        }

【问题讨论】:

  • 以及如何调用 getter 和 setter,请发布代码
  • 在构造函数内部,你不应该调用 setter & getter 或任何可覆盖的方法。
  • 你能发布赋值设置方法吗
  • 我已编辑请检查。
  • 按照这个你会得到空指针异常............

标签: java android setter getter serializable


【解决方案1】:

我强烈怀疑你在Item 的另一个实例上调用了getTitle(),而不是你在setTitle() 上调用的实例:

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

// Some other code

Item item2 = new Item();
String title = item2.getTitle(); // This will return null

【讨论】:

  • getTitle() 和 SetTitle() 都有相同的对象。请检查我编辑的代码。
  • @RahulPatel:不,你没有 - 在你使用 getTitle() 的代码中,你已经事先得到了 Item item = new Item(),就像我的回答一样。
  • 检查 for 循环条件 for (int i = 0; i
  • @RahulPatel:是的,这是在一个刚刚创建的项目上调用item.getTitle(),因此没有标题。换句话说,不是你之前调用setTitle的那个项目。
【解决方案2】:

试试这个,

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

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

【讨论】:

    【解决方案3】:

    如果您对setget 使用两个不同的对象,则可能会发生这种情况。检查您是否在同一个对象上调用settergetter

    编辑

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

    【讨论】:

    • @iozee 当@jon 已经发布了他的答案时,我碰巧写了答案。它确实发生了。我想这不是什么大罪行。
    • getTitle() 和 SetTitle() 都有相同的对象。请检查我编辑的代码
    • for 循环上方的这一行Item item = new Item() 怎么样?
    猜你喜欢
    • 2015-05-12
    • 1970-01-01
    • 2019-01-12
    • 2011-04-14
    • 2017-09-09
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多