-4

Firebase Recycler view

    FirebaseRecyclerAdapter<Post, MyViewHolder> fadapter = new FirebaseRecyclerAdapter<Post, MyViewHolder>
            (Post.class, R.layout.card_row, MyViewHolder.class, mDatabase) {
        @Override
        protected void populateViewHolder(MyViewHolder viewHolder, Post model, int position) {

        }
    };

View Holder Class

public static class MyViewHolder extends RecyclerView.ViewHolder {
    View mview;

    public MyViewHolder(View itemView) {
        super(itemView);
        //mview = itemView;

    }

}

Post.java

public class Post {
private String title;
private String image;
private String desc;

public Post(String title, String image, String desc) {
    this.title = title;
    this.image = image;
    this.desc = desc;
}

public Post() {
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

}

I am getting this error!

FATAL EXCEPTION: main
    Process: cf.socialstack.socialstack, PID: 22343                                                                            java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{cf.socialstack.socialstack/cf.socialstack.socialstack.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)' on a null object reference
            at com.firebase.ui.database.FirebaseArray.onCreate(FirebaseArray.java:69)
            at com.firebase.ui.database.ObservableSnapshotArray.addChangeEventListener(ObservableSnapshotArray.java:64)
            at com.firebase.ui.database.FirebaseRecyclerAdapter.startListening(FirebaseRecyclerAdapter.java:126)
            at com.firebase.ui.database.FirebaseRecyclerAdapter.<init>(FirebaseRecyclerAdapter.java:71)
            at com.firebase.ui.database.FirebaseRecyclerAdapter.<init>(FirebaseRecyclerAdapter.java:86)
            at com.firebase.ui.database.FirebaseRecyclerAdapter.<init>(FirebaseRecyclerAdapter.java:108)
            at cf.socialstack.socialstack.MainActivity$2.<init>(MainActivity.java:0)
            at cf.socialstack.socialstack.MainActivity.<init>(MainActivity.java:134)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.Class.newInstance(Class.java:1572)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
            at android.app.ActivityThread.access$800(ActivityThread.java:144) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5221) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693) `
K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33
Shubam Virdi
  • 144
  • 1
  • 4
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – F0XS May 28 '18 at 09:24

3 Answers3

0

Maybe your DatabaseReference is returning null, that's why you're getting the NullPointException.

Are you sure your database reference is pointing to some url that actually has some data to be filled in your recycler view?

0

I don't know what you want to achieve but this is the correct way to implement the FirebaseRecyclerView with Real-time database.

First Create your Query to load the Data.

 Query query = FirebaseDatabase.getInstance() .getReference().child("posts").limitToLast(50);

Create the Options for FirebaseRecyclerView Options.

FirebaseRecyclerOptions<Post> options = new FirebaseRecyclerOptions.Builder<Post>().setQuery(query, Post.class).build();

Create the Adapter

FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Post, MyViewHolder>(options){
   @Override 
     public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // Create a new instance of the ViewHolder, in this case we are using a custom // layout called R.layout.message for each item 
     View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.message, parent, false);
     return new MyViewHolder(view);
 }

 @Override 
 protected void onBindViewHolder(MyViewHolder holder, int position, Post model) { // Bind the Post object to the MyViewHolder // ...  
   } 
  };

As simple as anything attach this adapter to recycler view using recyclerView.setAdapter(adapter) and set the layout manager too.

coder3101
  • 3,920
  • 3
  • 24
  • 28
0

put your recycler adapter inside the onCreate method

Shubam Virdi
  • 144
  • 1
  • 4