Привет, столкнулся с ошибкой, уже все просмотрел, все перешерстил, ничего не могу поделать.
Выдает ошибку :
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();
}
});
}