-1

I have tried all i can but still cant solve the problem. i have also received most similar question but all the solution doesn't work. I'm trying to get the images from drawable into the image view, but its causing the application to crash with a null pointer exception This is my adapter class

 public class GroupAdapter extends RecyclerView.Adapter<GroupAdapter.GroupHolder>{

private Context context;
private List<GroupModel> groupList;
GroupModel model;

public GroupAdapter(Context context, List<GroupModel> groupList) {
    this.context = context;
    this.groupList = groupList;
}

@NonNull
@Override
public GroupHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    return new GroupHolder(LayoutInflater.from(parent.getContext())
            .inflate(R.layout.group_item, parent, false));
}

@Override
public void onBindViewHolder(@NonNull GroupHolder holder, int position) {
    final GroupModel groupModel = groupList.get(position);
    holder.setName(groupModel.getName());
    holder.setDesc(groupModel.getDescription());
    holder.setCourse(groupModel.getCourse());
    holder.setOwner(groupModel.getOwner());
    holder.setImage(groupModel.getImage());
    holder.view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(context, GroupMessage.class);
            context.startActivity(intent);
        }
    });

}

@Override
public int getItemCount() {
    return groupList.size();
}

public class GroupHolder extends RecyclerView.ViewHolder {

private final TextView  name;
private final TextView desc;
private final TextView course;
private final TextView owner;
private final CardView view;

private final ImageView image;

public GroupHolder(View itemView) {
    super(itemView);

            name = (TextView) itemView.findViewById(R.id.textView_grp_name );
            desc = (TextView) itemView.findViewById(R.id.textView_grp_description);
            course = (TextView) itemView.findViewById(R.id.textView_course);
            owner = (TextView) itemView.findViewById(R.id.textView_owner);
            image = (ImageView) itemView.findViewById(R.id.imageView_groups);
            view = (CardView) itemView.findViewById(R.id.group_view);
}

public void setName(String n){
    name.setText(n);
}

public void setDesc(String d){
    desc.setText(d);
}

public void setCourse(String c){
    course.setText(c);
}

public void setOwner(String o){
    owner.setText(o);
}

public void setImage(int i){
   // StorageReference reference = FirebaseStorage.getInstance().getReference().child(i);
    //Glide.with(context).load(reference).into(image);
    image.setImageDrawable(context.getResources().getDrawable(model.getImage()));
}
}
}

And this is get image method

 public int getImage() {

    return image;
}

and the error

  E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.arewagirlcoder.welearn, PID: 13590
java.lang.NullPointerException: Attempt to invoke virtual method 'int com.arewagirlcoder.welearn.GroupModel.getImage()' on a null object reference
    at com.arewagirlcoder.welearn.GroupAdapter$GroupHolder.setImage(GroupAdapter.java:106)
    at com.arewagirlcoder.welearn.GroupAdapter.onBindViewHolder(GroupAdapter.java:50)
    at com.arewagirlcoder.welearn.GroupAdapter.onBindViewHolder(GroupAdapter.java:25)
    at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673)
    at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714)
    at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)
    at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Zainab A.
  • 3
  • 2

3 Answers3

1

Put a check before setting your image Drawable

 public void setImage(int i){
       // StorageReference reference = FirebaseStorage.getInstance().getReference().child(i);
        //Glide.with(context).load(reference).into(image);
        Drawable imagedrawable = context.getResources().getDrawable(i); //use i instead of model.getImage(), you are already passing the drawable int
        if(imagedrawable !=null)
            image.setImageDrawable(imagedrawable);
        else
           Log.d(TAG, "image is null");
    }
shb
  • 5,957
  • 2
  • 15
  • 32
0

The problem is this line:

public void setImage(int i){
   image.setImageDrawable(context.getResources().getDrawable(model.getImage()));
}

Apparently your model variable is not being set, and I suspect it also might be redundant. Use this i variable that you've passed instead:

public void setImage(int i){
   image.setImageDrawable(context.getResources().getDrawable(i));
}

Explanation: In onBindViewHolder you have this:

final GroupModel groupModel = groupList.get(position);

Which does not set the model variable. But you do actually pass the current model's image resource to the method, so you can use that variable.

Vucko
  • 7,371
  • 2
  • 27
  • 45
  • Thanks. it solved the first error but created another one. Now it throws a resource not found exception – Zainab A. Oct 21 '18 at 12:00
  • No worries. That's another thing completely. Meaning there is no image with that particular `id` in your drawable folder. You should accept the answer since it solved the original issue. Check your logic for creating those `GroupModels`, you have to pass int ids which are in drawable. Or have some other way of setting images. – Vucko Oct 21 '18 at 12:12
  • It works now. Thanks – Zainab A. Oct 22 '18 at 11:14
0

I believe Vucko is right. I don't know how AS isn't throwing you a red error message for model.getImage() within your setImage(int i) method anyway because I don't see the model variable declared anywhere. EDIT: My mistake, I see it declared, but never set. Thus your problem.

You can read more about resources and getDrawable() here. If his answer doesn't work, than your model object isn't returning the proper drawable resource type. Valid types are on the Drawable resources page on the developer site.

If Vucko's solution doesn't work, please update with your object's code.

Mr.Drew
  • 939
  • 2
  • 9
  • 30