0

I really do not fully understand where the error is coming from. Can someone help me please? Thank you!

I post the LogCat and the code bellow!

================================================================================================================================================================================================

LogCat

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
    at com.musicapp.android.musicapp.MusicCustomAdapter.getItemCount(MusicCustomAdapter.java:42)

code:

public class MusicCustomAdapter extends RecyclerView.Adapter<MusicCustomAdapter.ViewHolder>{
    private ArrayList<Audio_Model> arrayListAudio;
    private Context context;

    public MusicCustomAdapter(ArrayList<Audio_Model> arrayListAudio, Context context) {
        this.arrayListAudio = arrayListAudio;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.song_display_model,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.Name_of_Song.setText(arrayListAudio.get(position).getName());
        holder.Name_of_Artist.setText(arrayListAudio.get(position).getArtist());
        holder.imageView_song_display_model.setImageBitmap(arrayListAudio.get(position).getSongCOmerPhoto());
    }

40   @Override
41    public int getItemCount() {
42        return arrayListAudio.size();
43    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        LinearLayout relativeLayout;
        TextView Name_of_Song;
        TextView Name_of_Artist;
        ImageView imageView_song_display_model;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            relativeLayout= itemView.findViewById(R.id.relativeLayoutId);
            Name_of_Song = itemView.findViewById(R.id.Name_of_Song);
            Name_of_Artist = itemView.findViewById(R.id.Name_of_Artist);
            imageView_song_display_model = itemView.findViewById(R.id.imageView_song_display_model);
        }
    }
}

1 Answer 1

2

The problem is that getItemCount() can be called before you provide items to the adapter, so you need to make sure it returns 0 if the list of items is null.

replace this code

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

with

@Override
public int getItemCount() {
    return arrayListAudio == null ? 0 : arrayListAudio.size();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.