java - how to make sure getter is not null -


it dump question sorry have been while not using java.

i have setter/getter class listmainitem want set , string getting null value.

    listmainitem object = new listmainitem();     string[] images = {"a","b","c"};      final arraylist<listmainitem> list = new arraylist<listmainitem>();     (int = 0; < images.length; ++i) {         object.setimages(images[i]);         list.add(object);         string test = object.getimages();         system.out.print(test); 

the value test being null doing wrong here

listmain class

public class listmainitem  implements parcelable{       string id;     //string name;     public string url;     string images;      listmainitem (parcel in){         this.id = in.readstring();         //   this.name = in.readstring();         this.url = in.readstring();         this.images= in.readstring();     }      public listmainitem() {     //    this.id = id;     //    this.url = url;           this.images = images;     }      @override     public int describecontents() {         return 0;     }       public void seturl(string url) {         this.url = url;     }      public string geturl() {         return url;     }      public void setid(string id) {         this.id = id;     }      public string getid() {         return id;     }      public void setimages(string image)     {         this.images=images;     }     public string getimages()     {         return images;     }     @override     public void writetoparcel(parcel dest, int flags) {         dest.writestring(this.id);         // dest.writestring(this.name);         dest.writestring(this.url);         dest.writestring(this.images);      }      public static final parcelable.creator<listmainitem> creator = new parcelable.creator<listmainitem>() {         public listmainitem createfromparcel(parcel in) {             return new listmainitem(in);         }          public listmainitem[] newarray(int size) {             return new listmainitem[size];         }     };    } 

here catch

public void setimages(string image)     {         this.images=images;     } 

you need change setimages(string image) setimages(string images)

also, i'm wondering if code compiles below line of code doesn't seems valid. initializing string array character array ?

string images[] = {'a','b','c'}; 

suggestion correction :

below line of code example of assignment itself. should comment assignment.

public listmainitem() { //    this.id = id; //    this.url = url;       this.images = images; } 

after each iteration, suppose add new object list, aren't doing here instead updating same object , adding reference. means element has same object reference , return same value. so, should creating new object in each iteration.

for (int = 0; < images.length; ++i) {     object = new listmainitem();     object.setimages(images[i]);     list.add(object); 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -