I am trying to retrieve data into a RecyclerView from Firebase Database but am experiencing this error java.lang.NullPointerException: Attempt to invoke virtual method. 'com.google.firebase.database.DatabaseReference com.google.firebase.database.DatabaseReference.child(java.lang.String)' on a null object reference
Below is my main activity class .
package oerrride.we.huzykamz.testingappfirebase;
import android.app.Dialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Button but;
private DatabaseReference mRef;
private MyAdapter adapter;
public RecyclerView recyclerView;
public List<ModelClass> movies_;
public Context c;
private LinearLayoutManager linearLayoutManager;
private DatabaseReference mMovieRef =mRef.child("Name");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
movies_ = new ArrayList<ModelClass>();
recyclerView =(RecyclerView) findViewById(R.id.recyclerview);
linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
mRef = FirebaseDatabase.getInstance().getReferenceFromUrl("xxxxxxxxxxxxxxxxxxxxxx");
mRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
LoadData(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
LoadData(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public void LoadData (DataSnapshot dataSnapshot){
for(DataSnapshot singlesnapshot :dataSnapshot.getChildren()){
String taskTitle =singlesnapshot.getValue(String.class);
movies_.add(new ModelClass(taskTitle));
adapter = new MyAdapter(MainActivity.this, movies_);
recyclerView.setAdapter(adapter);
}
}
}
And this is my Adapter class .
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
protected Context c;
List<ModelClass> movies;
public MyAdapter(Context c, List<ModelClass> movies) {
this.c = c;
this.movies = movies;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_activity,parent,false);
MyViewHolder holder = new MyViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.name.setText(movies.get(position).getName());
// PicassoClient.downloadImage(c,movies.get(position).getUrl(),holder.img);
}
@Override
public int getItemCount() {
return movies.size();
}
}
Below is my Console of Firebase database
Firebase Console data am trying to fetch
private DatabaseReference mMovieRef =mRef.child("Name");
mRef is not created yet so It is null and will throw NullPointerException
Solution:
Change your declaration to:
private DatabaseReference mMovieRef;
and assign value to it after you created mRef
mRef = FirebaseDatabase.getInstance().getReferenceFromUrl("xxxxxxxxxxxxxxxxxxxxxx");
mMovieRef = mRef.child("Name");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With