4

Error Image

Getting Error FATAL EXCEPTION: main Process: com.example.wuntu.tv_bucket, PID: 3895 java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference at com.example.wuntu.tv_bucket.Models.Cast.writeToParcel(Cast.java:136) at android.os.Parcel.writeParcelable(Parcel.java:1437) at android.os.Parcel.writeValue(Parcel.java:1343) at android.os.Parcel.writeList(Parcel.java:759) at android.os.Parcel.writeValue(Parcel.java:1365) at android.os.Parcel.writeArrayMapInternal(Parcel.java:686) at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330) at android.os.Bundle.writeToParcel(Bundle.java:1079) at android.os.Parcel.writeBundle(Parcel.java:711) at android.content.Intent.writeToParcel(Intent.java:8790) at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3112) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1541) at android.app.Activity.startActivityForResult(Activity.java:4284) at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79) at android.app.Activity.startActivityForResult(Activity.java:4231) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859) at android.app.Activity.startActivity(Activity.java:4568) at android.app.Activity.startActivity(Activity.java:4536) at com.example.wuntu.tv_bucket.Adapters.CastDetailAdapter$1.onClick(CastDetailAdapter.java:124) at android.view.View.performClick(View.java:5698) at android.widget.TextView.performClick(TextView.java:10908) at android.view.View$PerformClick.run(View.java:22557) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7231) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Getting error in sending arraylist of object from adapter to other activity. I wanna send my arraylist from onBindViewHolder method of Adapter to another activity but its is showing null exception error on the Cast Class in writetoParcel Method. How to send arraylist properly?

Cast Class

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

CastDetailAdapter Class

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)
    {
        a = 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();
    }
}
Shivam Kumar
  • 1,892
  • 2
  • 21
  • 33
Lakshya Punhani
  • 273
  • 1
  • 5
  • 17

2 Answers2

7

In your writeToParcel() method, you have

parcel.writeInt(id);

Since id is Integer, this is going to auto-unbox id. If id is null, this auto-unboxing will throw a NullPointerException.

Since there is no Parcel.writeInteger() method, you're going to have to record whether or not id is null in a separate write. Something like:

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

And to read it back out:

int hasId = in.readInt();

if (hasId == 1) {
    id = in.readInt();
}
else {
    id = null;
}
Ben P.
  • 52,661
  • 6
  • 95
  • 123
1

The order in which you read the values from the parcel has to be the same as the order it was written to it.

Try:

protected Cast(Parcel in) {
    name = in.readString();
    profilePath = in.readString();
    character = in.readString();
    id = in.readInt();
}
Maarten van Tjonger
  • 1,867
  • 1
  • 10
  • 15
  • Its not working. Error is heading towards @Override public void writeToParcel(Parcel parcel, int i) { parcel.writeString(character); parcel.writeInt(id); parcel.writeString(name); parcel.writeString(profilePath); } At parcel.writeInt(id) Line – Lakshya Punhani Jul 25 '17 at 16:56
  • Ah, it looks like your id Integer is null, which causes the exception. Make sure you initialize it with a value. – Maarten van Tjonger Jul 25 '17 at 17:05
  • Thank you, somewhere in the array list, id was not initialized. – Lakshya Punhani Jul 25 '17 at 17:31