我有一个返回NullPoint异常的类。我没有得到的是,有关的变量已被实例化。我不明白为什么它返回为Null。必须使用对象模型填充modelList。然后,这个modelList是我在适配器类中的参数以及另一个Activity。该modelList包含从Firestore中选取的数据值,应该对其进行检索才能在Recyclerview中显示。
Java类
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.List;
public class ViewRegisteredProperty extends AppCompatActivity {
RecyclerView recyclerView;
List<ModelOwnerPropertyList> modelList;
FirebaseFirestore db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_registered_property);
//instance of firebasefirestore
db = FirebaseFirestore.getInstance();
showData();
}
public void showData(){
db.collection("Companies")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for (QueryDocumentSnapshot document : task.getResult()) {
ModelOwnerPropertyList model = new ModelOwnerPropertyList(document.getString("Address"),
document.getString("CompanyName"),
document.getString("TotalTenants"),
document.getString("ownerName"));
modelList.add(model);
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(), "No data collected", Toast.LENGTH_SHORT).show();
}
});
}
public void getData(){
db.collection("Companies")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
ModelOwnerPropertyList model = new ModelOwnerPropertyList(document.getString("Address"),
document.getString("CompanyName"),
document.getString("TotalTenants"),
document.getString("ownerName"));
modelList.add(model);
}
MyAdapter adapter = new MyAdapter(ViewRegisteredProperty.this ,modelList);
recyclerView.setAdapter(adapter);
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(), "No data collected", Toast.LENGTH_SHORT).show();
}
});
}
}
StackTrace
02-21 17:06:33.215 28076-28076/com.example.rent E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.rent, PID: 28076
java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
at com.example.rent.ViewRegisteredProperty$2.onComplete(ViewRegisteredProperty.java:57)
at com.google.android.gms.tasks.zzj.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:227)
at android.app.ActivityThread.main(ActivityThread.java:6102)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:961)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:822)