I’m having error in Glide,
this error is this
can anyone sort this error out pls i need help
its build perfectly fine but im facing the error at runtime
this line is causing main problem
Glide.with(context).load(R.drawable.disc).into(holder.sound_dis);
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.paradox.projectsp3, PID: 27908
java.lang.NullPointerException: Argument must not be null
at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:29)
at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:23)
at com.bumptech.glide.RequestBuilder.into(RequestBuilder.java:768)
at com.paradox.projectsp3.Adapter.DemoAdapter.onBindViewHolder(DemoAdapter.java:44)
at com.paradox.projectsp3.Adapter.DemoAdapter.onBindViewHolder(DemoAdapter.java:19)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:23304)
at android.view.ViewGroup.layout(ViewGroup.java:6539)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
at android.view.View.layout(View.java:23304)
at android.view.ViewGroup.layout(ViewGroup.java:6539)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:23304)
at android.view.ViewGroup.layout(ViewGroup.java:6539)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:536)
at android.view.View.layout(View.java:23304)
at android.view.ViewGroup.layout(ViewGroup.java:6539)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:23304)
at android.view.ViewGroup.layout(ViewGroup.java:6539)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1841)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:23304)
at android.view.ViewGroup.layout(ViewGroup.java:6539)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:871)
at android.view.View.layout(View.java:23304)
at android.view.ViewGroup.layout(ViewGroup.java:6539)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3979)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3380)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2345)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9046)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1066)
at android.view.Choreographer.doCallbacks(Choreographer.java:889)
at android.view.Choreographer.doFrame(Choreographer.java:816)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1051)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:264)
at android.app.ActivityThread.main(ActivityThread.java:8282)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:632)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)
this is my adapter im using
package com.paradox.projectsp3.Adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.paradox.projectsp3.Model.MediaObject;
import com.paradox.projectsp3.R;
import java.util.List;
public class DemoAdapter extends RecyclerView.Adapter<DemoAdapter.DemoViewHolder> {
List<MediaObject> mediaObjectList;
private ImageView imageView;
public Context context;
public DemoAdapter(List<MediaObject> mediaObjectList,Context context) {
this.mediaObjectList = mediaObjectList;
this.context = context;
}
@NonNull
@Override
public DemoViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup , int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_main,viewGroup,false);
return new DemoViewHolder(view);
}
@Override
public void onBindViewHolder( DemoViewHolder holder, int position) {
MediaObject mediaObject = mediaObjectList.get(position);
Glide.with(context).load(R.drawable.disc).into(holder.sound_dis);
}
@Override
public int getItemCount() {
return mediaObjectList.size();
}
public class DemoViewHolder extends RecyclerView.ViewHolder {
private ImageView sound_dis;
public DemoViewHolder(@NonNull View itemView) {
super(itemView);
ImageView sound_dis = (ImageView) itemView.findViewById(R.id.imageView3);
}
}
}
2
Answers
Your sound_dis is null because you accidentally created a local variable in your DemoViewHolder constructor and stored the ImageView reference in it. The one declared in the class has no value (null).
In your DemoViewholder function remove ImageView data type as you have already declared it in your DemoviewHolder class .
Your DemoViewHolder constructor should be like this :
This is not an error in glide , actually you have declared imageview wrongly. Your DemoViewHolder will be like this , then it will work fine.