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

        }
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
Rahul Patel
  • 3,823
  • 5
  • 30
  • 46

3 Answers3

1

I strongly suspect you've called getTitle() on a different instance of Item than the one you called setTitle() on:

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

// Some other code

Item item2 = new Item();
String title = item2.getTitle(); // This will return null
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I have same object for both getTitle() and SetTitle().please check my edited code. – Rahul Patel Jun 21 '12 at 07:26
  • @RahulPatel: No you don't - in the code where you're using `getTitle()`, you've got `Item item = new Item()` just beforehand, just like in my answer. – Jon Skeet Jun 21 '12 at 07:33
  • check into for loop condition for (int i = 0; i < item.getTitle().length(); i++) – Rahul Patel Jun 21 '12 at 08:11
  • @RahulPatel: Yes, that's calling `item.getTitle()` on an item which has just been created, and therefore has no title. In other words, *not* the item which you called `setTitle` on before. – Jon Skeet Jun 21 '12 at 08:48
0

This may happen if you using 2 different object to set and get. Check if you are calling the setter and getter on the same object.

EDIT

the object used to set title i.e. item.setTitle(content.toString()); is not the same as the item object created just before the for loop. You are recreating/re-initializing the item object by using Item item = new Item() before the for loop. Make a global item object and create it only once.

Arun George
  • 18,352
  • 4
  • 28
  • 28
0

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.

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75