So the code may seem very messy,Here is a equipmentReport page,I want to do is using a spinner so the users can choose what status they want to check(in the field name "狀態"),this is what the spinner looks like,the 4 status,but in the firestore theres only one existing now
<string-array name="reportst">
<item>待處理</item>
<item>處理中</item>
<item>待驗收</item>
<item>已完成</item> //THIS IS THE ONE EXIST NOW
</string-array>
right now there is only one doc with the status field "狀態" at "已完成",which is meaning finished, but I want the users to check out the other one even it doesnt exist(show empty listview,not crashing)
my code is written below
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinvar = parent.getItemAtPosition(position).toString();
DocumentReference docRef2 = firebaseFirestore.collection("users").document(userID);
docRef2.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d("TAG", "DocumentSnapshot data: " + document.getData());
recID = document.getString("recID");
firebaseFirestore.collection("equipmentReport").whereEqualTo("recID",recID).whereEqualTo("狀態",spinvar).orderBy("createdAt", Query.Direction.DESCENDING).addSnapshotListener((documentSnapshots, error) -> {
ar.clear();
**for (DocumentSnapshot snapshot : documentSnapshots){**//THE ERROR
if(snapshot != null){
if(snapshot.exists()){
idlv = snapshot.getId();
urll = snapshot.getString("照片");
if (snapshot.getDate("createdAt") != null) {
// Date timestamp = snapshot.getDate("createdAt");
//Do what you need to do with this timestamp
Timestamp timestamp = (Timestamp) snapshot.getData().get("createdAt");
Date date = timestamp.toDate();
date2 = date.toString();
}
ar.add(new itemAnnounce(R.drawable.pp, snapshot.getString("報修地點"),"反映於 "+date2,"事由:"+snapshot.getString("申報事由"),"狀態:"+snapshot.getString("狀態"),idlv,url));
}else{
}
}else{
}
}
adapterAnnounce adapterAnnounce = new adapterAnnounce(getApplicationContext(), R.layout.list_row_announce, ar);
adapterAnnounce.notifyDataSetChanged();
lv1.setAdapter(adapterAnnounce);
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object selectedObj =adapterAnnounce.getItem(position).getId();// this will get you selected obj of itemAnnounce
String obj = (String)selectedObj.toString();
DocumentReference docRef = firebaseFirestore.collection("equipmentReport").document(obj);
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d("TAG", "DocumentSnapshot data: " + document.getData());
String stat = document.getString("狀態");
if(stat.equals("待驗收")){
Intent i = new Intent(MyEQreport.this,EQreportRate.class);
i.putExtra("eqrId",obj);
startActivity(i);
}else{
}
} else {
Log.d("TAG", "No such document");
}
} else {
Log.d("TAG", "get failed with ", task.getException());
}
}
});
}
});
});
} else {
Log.d("TAG", "No such document");
}
} else {
Log.d("TAG", "get failed with ", task.getException());
}
}
});
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
the error is
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.districtapp, PID: 5916
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Iterator com.google.firebase.firestore.QuerySnapshot.iterator()' on a null object reference
at com.example.districtapp.MyEQreport$3.lambda$onComplete$0$MyEQreport$3(MyEQreport.java:167)
at com.example.districtapp.-$$Lambda$MyEQreport$3$hc9R1wp67pt9ujCkUwXliV_GHMs.onEvent(Unknown Source:4)
at com.google.firebase.firestore.Query.lambda$addSnapshotListenerInternal$2$Query(Query.java:1126)
at com.google.firebase.firestore.-$$Lambda$Query$JWhMgzcsIac1Z-exZj1pTDRisJg.onEvent(Unknown Source:6)
at com.google.firebase.firestore.core.AsyncEventListener.lambda$onEvent$0$AsyncEventListener(AsyncEventListener.java:42)
at com.google.firebase.firestore.core.-$$Lambda$AsyncEventListener$DNkggu2LY54oguDvcp-QtRg6Sfg.run(Unknown Source:6)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
I know it needs some snapshot exception but its not working at all
Move the null
check to documentSnapshots
:
if(documentSnapshots != null){
for (DocumentSnapshot snapshot : documentSnapshots) {
if(snapshot.exists()){
idlv = snapshot.getId();
urll = snapshot.getString("照片");
if (snapshot.getDate("createdAt") != null) {
// Date timestamp = snapshot.getDate("createdAt");
//Do what you need to do with this timestamp
Timestamp timestamp = (Timestamp) snapshot.getData().get("createdAt");
Date date = timestamp.toDate();
date2 = date.toString();
}
ar.add(new itemAnnounce(R.drawable.pp, snapshot.getString("報修地點"),"反映於 "+date2,"事由:"+snapshot.getString("申報事由"),"狀態:"+snapshot.getString("狀態"),idlv,url));
} else {
}
}
} else {
}