So I was trying to show images from firebase to recyclerview, but it threw me an error. Error says E/nitecompetitiv: [qarth_debug:] get PatchStore::createDisableExceptionQarthFile method fail. E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.fortnitecompetitive, PID: 15205 java.lang.NullPointerException: Attempt to invoke virtual method ‘void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)’ on a null object reference at com.example.fortnitecompetitive.NewsActivity$1.onDataChange(NewsActivity.java:66) at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75) at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63) at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55) at android.os.Handler.handleCallback(Handler.java:907) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:216) at android.app.ActivityThread.main(ActivityThread.java:7625) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987). I would appreciate help so much.
public class NewsActivity extends AppCompatActivity { FirebaseDatabase mDatabase; DatabaseReference mRef; FirebaseStorage mStorage; RecyclerView recyclerView; List<Post> postList; private RecyclerView mRecyclerView; private ImageAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_news); getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); getSupportActionBar().setCustomView(R.layout.action_bar); mDatabase = FirebaseDatabase.getInstance(); mRef = FirebaseDatabase.getInstance().getReference("Posts"); mStorage = FirebaseStorage.getInstance(); recyclerView = findViewById(R.id.recycler_news); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(this)); postList = new ArrayList<>(); mRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot snapshot) { for (DataSnapshot postSnapshot : snapshot.getChildren()){ Post post = postSnapshot.getValue(Post.class); postList.add(post); } mAdapter = new ImageAdapter(NewsActivity.this, postList); mRecyclerView.setAdapter(mAdapter); } @Override public void onCancelled(@NonNull DatabaseError error) { } }); } }
Adapter
import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.squareup.picasso.Picasso; import java.util.List; public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> { private Context mContext; private List<Post> mUploads; public ImageAdapter(Context context, List<Post> uploads){ mContext = context; mUploads = uploads; } @NonNull @Override public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View v = LayoutInflater.from(mContext).inflate(R.layout.news_row,parent,false); return new ImageViewHolder(v); } @Override public void onBindViewHolder(@NonNull ImageAdapter.ImageViewHolder holder, int position) { Post post = mUploads.get(position); holder.textViewName.setText(post.getmTitle()); Picasso.get().load(post.getmImage()).fit().centerCrop().into(holder.imageView); } @Override public int getItemCount() { return mUploads.size(); } public class ImageViewHolder extends RecyclerView.ViewHolder{ public TextView textViewName; public ImageView imageView; public ImageViewHolder (View itemView) { super (itemView); textViewName = itemView.findViewById(R.id.newsTitle); imageView = itemView.findViewById(R.id.newsImage); } } }
Posts class
public class Post { public String mTitle; public String mImage; public Post(){ } public Post(String mTitle, String mImage){ this.mTitle = mTitle; this.mImage = mImage; } public String getmImage(){ return mImage; } public String getmTitle() { return mTitle; } }
Advertisement
Answer
You have two recycler view instance in your activity.
RecyclerView recyclerView; private RecyclerView mRecyclerView;//remove this line and set adapter for above recyclerView instance recyclerView.setAdapter(mAdapter);