Search code examples
javaandroidjsoninstancepojo

In android my pojo class instance are getting null value


i have created a pojo class.. where i store data which i am getting from api..

In MainActivity.java i am using loopj for parsing json data

storeList = new ArrayList<AllStore>();
                try {
                    JSONArray jsonArray = new JSONArray(new String(responseBody));
                    for (int i = 0; i < jsonArray.length(); i++) {
                        store = new AllStore();

                        store.setLocation(jsonArray.getJSONObject(i).getString("storeName"));
                        store.setAddress(jsonArray.getJSONObject(i).getString("address"));

my pojo class

AllStore.java

public class AllStore{

String storeName;
String address;

public AllStore(String storeName, String address){
this.storeName= storeName;
this.location = location;
}

public AllStore{

}

public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

public String getStoreName() {
        return storeName;
    }

    public void setStoreName(String storeName) {
        this.storeName = storeName;
    }

}

In other java classes.. rather then adapter class when i am doing

AllStore allStore = new Allstore();

mText.setText(allStore.getStoreName);

Here allStore.getStoreName value is null showing.. how to solve this..

Please help


Solution

  • AllStore allStore = new Allstore();
    
    mText.setText(allStore.getStoreName());
    

    Here you are creating a new object Allstore with the default constructor

    public AllStore{
    
    }
    

    If you call .getStoreName it will return null because you haven´t set any.

    In the other method, you have to add the store object in te array list

    store.setLocation(jsonArray.getJSONObject(i).getString("storeName"));        
    store.setAddress(jsonArray.getJSONObject(i).getString("address"));
    storeList.add(store);
    

    Later if you want to use the name of the item in the list you can do:

    mText.setText(storelist.get(index).getStoreName());
    

    Where index is the position in the list of the item.