android - Attempt to invoke virtual method 'int.java.lang.Integer.intValue()' on a null object reference at Cast.writetoParcel -


this question has answer here:

error image

getting error fatal exception: main process: com.example.wuntu.tv_bucket, pid: 3895 java.lang.nullpointerexception: attempt invoke virtual method 'int java.lang.integer.intvalue()' on null object reference @ com.example.wuntu.tv_bucket.models.cast.writetoparcel(cast.java:136) @ android.os.parcel.writeparcelable(parcel.java:1437) @ android.os.parcel.writevalue(parcel.java:1343) @ android.os.parcel.writelist(parcel.java:759) @ android.os.parcel.writevalue(parcel.java:1365) @ android.os.parcel.writearraymapinternal(parcel.java:686) @ android.os.basebundle.writetoparcelinner(basebundle.java:1330) @ android.os.bundle.writetoparcel(bundle.java:1079) @ android.os.parcel.writebundle(parcel.java:711) @ android.content.intent.writetoparcel(intent.java:8790) @ android.app.activitymanagerproxy.startactivity(activitymanagernative.java:3112) @ android.app.instrumentation.execstartactivity(instrumentation.java:1541) @ android.app.activity.startactivityforresult(activity.java:4284) @ android.support.v4.app.basefragmentactivityjb.startactivityforresult(basefragmentactivityjb.java:50) @ android.support.v4.app.fragmentactivity.startactivityforresult(fragmentactivity.java:79) @ android.app.activity.startactivityforresult(activity.java:4231) @ android.support.v4.app.fragmentactivity.startactivityforresult(fragmentactivity.java:859) @ android.app.activity.startactivity(activity.java:4568) @ android.app.activity.startactivity(activity.java:4536) @ com.example.wuntu.tv_bucket.adapters.castdetailadapter$1.onclick(castdetailadapter.java:124) @ android.view.view.performclick(view.java:5698) @ android.widget.textview.performclick(textview.java:10908) @ android.view.view$performclick.run(view.java:22557) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:158) @ android.app.activitythread.main(activitythread.java:7231) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1230) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1120)

getting error in sending arraylist of object adapter other activity. wanna send arraylist onbindviewholder method of adapter activity showing null exception error on cast class in writetoparcel method. how send arraylist properly?

public class cast implements parcelable {  @serializedname("cast_id") @expose private integer castid; @serializedname("character") @expose private string character; @serializedname("credit_id") @expose private string creditid; @serializedname("gender") @expose private integer gender; @serializedname("id") @expose private integer id; @serializedname("name") @expose private string name; @serializedname("order") @expose private integer order; @serializedname("profile_path") @expose private string profilepath;  public cast(){  }  protected cast(parcel in) {     character = in.readstring();     id = in.readint();     name = in.readstring();     profilepath = in.readstring(); }  public static final creator<cast> creator = new creator<cast>() {     @override     public cast createfromparcel(parcel in) {         return new cast(in);     }      @override     public cast[] newarray(int size) {         return new cast[size];     } };  public integer getcastid() {     return castid; }  public void setcastid(integer castid) {     this.castid = castid; }  public string getcharacter() {     return character; }  public void setcharacter(string character) {     this.character = character; }  public string getcreditid() {     return creditid; }  public void setcreditid(string creditid) {     this.creditid = creditid; }  public integer getgender() {     return gender; }  public void setgender(integer gender) {     this.gender = gender; }  public integer getid() {     return id; }  public void setid(integer id) {     this.id = id; }  public string getname() {     return name; }  public void setname(string name) {     this.name = name; }  public integer getorder() {     return order; }  public void setorder(integer order) {     this.order = order; }  public string getprofilepath() {     return profilepath; }  public void setprofilepath(string profilepath) {     this.profilepath = profilepath; }  @override public int describecontents() {     return 0; }  @override public void writetoparcel(parcel parcel, int i) {     parcel.writestring(name);     parcel.writestring(profilepath);     parcel.writestring(character);     parcel.writeint(id); } 

}

public class castdetailadapter extends recyclerview.adapter<recyclerview.viewholder> {  private arraylist<cast> detailarraylist = new arraylist<>() ; private urlconstants urlconstants = urlconstants.getsingletonref(); private cast cast; private final int view_item = 0; private final int view_prog = 1; private context context; movieview a; arraylist<cast> fullarraylist = new arraylist<>();  public castdetailadapter(movieview movieview, arraylist<cast> detailarraylist,arraylist<cast> subcastarraylist) {     = movieview;     this.detailarraylist = subcastarraylist;     this.fullarraylist = detailarraylist; }   public class myviewholder1 extends recyclerview.viewholder {     imageview cast_profile_picture;     textview cast_name,cast_character_name;      public myviewholder1(view view)     {         super(view);         cast_profile_picture = (imageview) view.findviewbyid(r.id.thumbnail);         cast_name = (textview) view.findviewbyid(r.id.title);         cast_character_name = (textview) view.findviewbyid(r.id.count);     } }  public class footerviewholder1 extends recyclerview.viewholder {     textview view_more;      public footerviewholder1(view itemview) {         super(itemview);         view_more = (textview) itemview.findviewbyid(r.id.view_more);       } }  @override public int getitemviewtype(int position) {     if (ispositionitem(position))         return view_item;     return view_prog; }  private boolean ispositionitem(int position) {     return position != getitemcount() -1; } @override public recyclerview.viewholder oncreateviewholder(viewgroup parent, int viewtype) {     context = parent.getcontext();      if (viewtype == view_item)     {         view v =  layoutinflater.from(parent.getcontext())                 .inflate(r.layout.cast_details, parent, false);         return new myviewholder1(v);      } else if (viewtype == view_prog){         view v = layoutinflater.from(parent.getcontext())                 .inflate(r.layout.footer_layout_movie_details, parent, false);         return new footerviewholder1(v);     }     return null;  }  @override public void onbindviewholder(recyclerview.viewholder holder, int position) {     if(holder instanceof myviewholder1)     {         cast = detailarraylist.get(position);         ((myviewholder1)holder).cast_character_name.settext(cast.getcharacter());         ((myviewholder1)holder).cast_name.settext(cast.getname());         string url3 = urlconstants.url_image + cast.getprofilepath();         picasso.with(context)                 .load(url3)                 .into(((myviewholder1)holder).cast_profile_picture);     }     else if (holder instanceof footerviewholder1)     {         ((footerviewholder1)holder).view_more.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view)             {                 intent intent = new intent(context,castviewactivity.class);                 intent.putparcelablearraylistextra("list",fullarraylist);                 context.startactivity(intent);             }         });     } }  @override public int getitemcount() {     return this.detailarraylist.size(); } 

}

in writetoparcel() method, have

parcel.writeint(id); 

since id integer, going auto-unbox id. if id null, auto-unboxing throw nullpointerexception.

since there no parcel.writeinteger() method, you're going have record whether or not id null in separate write. like:

if (id == null) {     dest.writeint(0); } else {     dest.writeint(1);     dest.writeint(id); } 

and read out:

int hasid = in.readint();  if (hasid == 1) {     id = in.readint(); } else {     id = null; } 

Comments

Popular posts from this blog

javascript - Replicate keyboard event with html button -

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Web audio api 5.1 surround example not working in firefox -