Search code examples
javaandroidfirebase-realtime-databaseandroid-recyclerviewfirebaseui

Android Studio - (java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.ChildEventListener')


I am trying to retrieve data from Firebase real time db to display in RecyclerView of Android Studio, however after running the app on emulator, it only managed to display 1 row of data so i checked the logcat and this is the error

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.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

I am only attempting to retrieve the hawker's name and description for now

MainActivity.java

public class MainActivity extends AppCompatActivity {

DatabaseReference ref;
private FirebaseRecyclerOptions<Hawker> options;
private FirebaseRecyclerAdapter<Hawker, myViewHolder> adapter;
private RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ref = FirebaseDatabase.getInstance().getReference().child("hawkers");

    recyclerView=findViewById(R.id.recycler);
    recyclerView.setHasFixedSize(false);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    options = new FirebaseRecyclerOptions.Builder<Hawker>().setQuery(ref, Hawker.class).build();
    adapter = new FirebaseRecyclerAdapter<Hawker, myViewHolder>(options) {

        @Override
        protected void onBindViewHolder(@NonNull myViewHolder holder, int position, @NonNull Hawker model) {
            holder.textViewID.setText(model.getName());
            holder.textViewName.setText(model.getDescription());
        }

        @NonNull
        @Override
        public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_view_layout,parent,false);
            return new myViewHolder(v);
        }
    };
    adapter.startListening();
    recyclerView.setAdapter(adapter);
}
}

myViewHolder.java

public class myViewHolder extends RecyclerView.ViewHolder{
TextView textViewID, textViewName;

public myViewHolder(@NonNull View itemView) {
    super(itemView);
    textViewID = itemView.findViewById(R.id.textViewID);
    textViewName = itemView.findViewById(R.id.textViewName);
}
}

Hawker.java

public class Hawker {
private String name;
private String description;

public Hawker() {
}

public Hawker(String name, String description) {
    this.name = name;
    this.description = description;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

}

the full error in logcat

2021-03-05 00:24:30.691 6466-6466/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test, PID: 6466
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.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:3449)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
 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:54)
    at com.firebase.ui.common.BaseObservableSnapshotArray.addChangeEventListener(BaseObservableSnapshotArray.java:97)
    at com.firebase.ui.database.FirebaseRecyclerAdapter.startListening(FirebaseRecyclerAdapter.java:52)
    at com.example.test.MainActivity.onCreate(MainActivity.java:67)
    at android.app.Activity.performCreate(Activity.java:8000)
    at android.app.Activity.performCreate(Activity.java:7984)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:223) 
    at android.app.ActivityThread.main(ActivityThread.java:7656) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
2021-03-05 00:24:30.703 6466-6466/? I/Process: Sending signal. PID: 6466 SIG: 9

Output in emulator These are the data in firebase


Solution

  • i think you need to check whether your Hawker class is matched with the data in firebase or not, the attribute name and number of attributes in your java class should match to the firebase