[英]Issue with getters and setters Methods
Hello I am working on the google NewsReader
Application Using SAX
, Here I am using getters
and setter
s methods for get the data. 您好,我正在使用
SAX
google NewsReader
应用程序,在这里,我正在使用getters
和setter
的方法来获取数据。
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 . 但是我有一个问题,我可以将值添加到
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);
}
Output into Logcat: 输出到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. 在这里,我得到了
item.getTitle().length()
值为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);
}
I strongly suspect you've called getTitle()
on a different instance of Item
than the one you called setTitle()
on: 我强烈怀疑您在另一个
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
This may happen if you using 2 different object to set
and get
. 如果使用2个不同的对象进行
set
和get
则可能会发生这种情况。 Check if you are calling the setter
and getter
on the same object. 检查是否在同一对象上调用了
setter
和getter
。
EDIT 编辑
the object used to set title ie item.setTitle(content.toString());
用于设置标题的对象,即
item.setTitle(content.toString());
is not the same as the item
object created just before the for
loop. 与
for
循环之前创建的item
对象不同。 You are recreating/re-initializing the item
object by using Item item = new Item()
before the for
loop. 您正在通过
for
循环之前使用Item item = new Item()
重新创建/重新初始化item
对象。 Make a global item
object and create it only once. 创建一个全局
item
对象,并仅创建一次。
Try this, 尝试这个,
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();
}
}
Please try to check the instances on which you are setting and getting. 请尝试检查您要设置和获取的实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.