Answer the question
In order to leave comments, you need to log in
Error on a null object reference in android studio, how to fix?
Hi, I ran into an error, I've already looked through everything, I've redone everything, I can't do anything.
Gives an error message :
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.google.firebase.firestore.QuerySnapshot.getDocumentChanges()' on a null object reference
Process: com.vuradoapps.mycity, PID: 8801
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.google.firebase.firestore.QuerySnapshot.getDocumentChanges()' on a null object reference
at com.vuradoapps.mycity.Tasks_mini_app_Activity$3.onEvent(Tasks_mini_app_Activity.java:86)
at com.vuradoapps.mycity.Tasks_mini_app_Activity$3.onEvent(Tasks_mini_app_Activity.java:82)
at com.google.firebase.firestore.Query.lambda$addSnapshotListenerInternal$2$com-google-firebase-firestore-Query(Query.java:1131)
at com.google.firebase.firestore.Query$$ExternalSyntheticLambda2.onEvent(Unknown Source:6)
at com.google.firebase.firestore.core.AsyncEventListener.lambda$onEvent$0$com-google-firebase-firestore-core-AsyncEventListener(AsyncEventListener.java:42)
at com.google.firebase.firestore.core.AsyncEventListener$$ExternalSyntheticLambda0.run(Unknown Source:6)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8653)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.firebase.firestore.DocumentChange;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.ListenerRegistration;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QuerySnapshot;
import com.vuradoapps.mycity.Adapter.ToDoAdapter;
import com.vuradoapps.mycity.Model.ToDoModel;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class Tasks_mini_app_Activity extends AppCompatActivity implements OnDialogChooce{
private RecyclerView recyclerView;
private FloatingActionButton mFab;
private FirebaseFirestore firebaseFirestore;
private ToDoAdapter adapter;
private List<ToDoModel> mList;
private Query query;
private ListenerRegistration listenerRegistration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tasks_mini_app);
Toolbar toolbar = findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onBackPressed();
}
});
recyclerView = findViewById(R.id.recycleView);
mFab = findViewById(R.id.floatingActionButton);
firebaseFirestore = FirebaseFirestore.getInstance();
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(Tasks_mini_app_Activity.this));
mFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Add_new_task.newInstance().show(getSupportFragmentManager(), Add_new_task.TAG);
}
});
mList = new ArrayList<>();
adapter = new ToDoAdapter(Tasks_mini_app_Activity.this, mList);
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new TouchHelper(adapter));
itemTouchHelper.attachToRecyclerView(recyclerView);
recyclerView.setAdapter(adapter);
showData();
}
private void showData(){
query = firebaseFirestore.collection("task").orderBy("time" , Query.Direction.DESCENDING);
listenerRegistration = query.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
for (DocumentChange documentChange : value.getDocumentChanges()){
if (documentChange.getType() == DocumentChange.Type.ADDED){
String id = documentChange.getDocument().getId();
ToDoModel toDoModel = documentChange.getDocument().toObject(ToDoModel.class).withId(id);
mList.add(toDoModel);
adapter.notifyDataSetChanged();
}
}
listenerRegistration.remove();
}
});
}
@Override
public void onDialogClose(DialogInterface dialogInterface) {
mList.clear();
showData();
adapter.notifyDataSetChanged();
}
}
private void showData(){
query = firebaseFirestore.collection("task").orderBy("time" , Query.Direction.DESCENDING);
listenerRegistration = query.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
for (DocumentChange documentChange : value.getDocumentChanges()){
if (documentChange.getType() == DocumentChange.Type.ADDED){
String id = documentChange.getDocument().getId();
ToDoModel toDoModel = documentChange.getDocument().toObject(ToDoModel.class).withId(id);
mList.add(toDoModel);
adapter.notifyDataSetChanged();
}
}
listenerRegistration.remove();
}
});
}
Answer the question
In order to leave comments, you need to log in
you have the value parameter in onEvent marked as Nullable, i.e. it is assumed that null can arrive
there. However, you boldly pull value.getDocumentChanges() without any checks.
Naturally, in a situation when null arrives in onEvent, everything falls down for you.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question