简体   繁体   English

Firebase 从 DataSnapShot 检索存储的 ArrayList 给出 NullPointerException

[英]Firebase retrieve stored ArrayList from DataSnapShot gives NullPointerException

My firebase database looks likes this我的 firebase 数据库看起来像这样

Firebase 图像

The blue rectangle is the data I want to retrieve.蓝色矩形是我要检索的数据。

Modal class for the blue rectangle looks like this蓝色矩形的模态类看起来像这样

public class TripToCompany implements Serializable {

String tripDate;
String companyName;
String vehicleNo;
boolean isFinished;
String firstPickUp;
String inTime;
ArrayList<EmpToCompany> emptoCompanyList;

public TripToCompany() {
}

public TripToCompany(String tripDate, String companyName, String vehicleNo, boolean isFinished, String firstPickUp, String inTime, ArrayList<EmpToCompany> emptoCompanyList) {
    this.tripDate = tripDate;
    this.companyName = companyName;
    this.vehicleNo = vehicleNo;
    this.isFinished = isFinished;
    this.firstPickUp = firstPickUp;
    this.inTime = inTime;
    this.emptoCompanyList = emptoCompanyList;
}

public TripToCompany(String tripDate, String companyName, String vehicleNo) {
    this.tripDate = tripDate;
    this.companyName = companyName;
    this.vehicleNo = vehicleNo;
    this.isFinished = false;
    this.inTime = "-";
    this.emptoCompanyList = new ArrayList<>();
}

public String getTripDate() {
    return tripDate;
}

public void setTripDate(String tripDate) {
    this.tripDate = tripDate;
}

public String getCompany() {
    return companyName;
}

public void setCompany(String companyName) {
    this.companyName = companyName;
}

public String getVehicleNo() {
    return vehicleNo;
}

public void setVehicleNo(String vehicleNo) {
    this.vehicleNo = vehicleNo;
}

public boolean isFinished() {
    return isFinished;
}

public void setFinished(boolean isFinished) {
    this.isFinished = isFinished;
}

public String getFirstPickUp() {
    return firstPickUp;
}

public void setFirstPickUp(String firstPickUp) {
    this.firstPickUp = firstPickUp;
}

public String getInTime() {
    return inTime;
}

public void setInTime(String inTime) {
    this.inTime = inTime;
}

public ArrayList<EmpToCompany> getEmptoCompanyList() {
    return emptoCompanyList;
}

public void setEmptoCompanyList(ArrayList<EmpToCompany> emptoCompanyList) {
    emptoCompanyList = emptoCompanyList;
}

public void addEmpToCompanyList(EmpToCompany etc) {
    if (emptoCompanyList.size() == 0) {
        firstPickUp = etc.getCabInTime();
    }
    emptoCompanyList.add(etc);

  }
}

I am fetching the data from firebase using the standard query.我正在使用标准查询从 firebase 获取数据。 Here is the code这是代码

    FirebaseDatabase defaultDatabase = FirebaseDatabase.getInstance();
    DatabaseReference mRef = defaultDatabase.getReference("data");
    Query query = mRef.child("toComp/" + companyId + "/" + fromDate + "/" + shiftTimeId + "/");
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot ds) {

            if (ds.exists()) {
                System.out.println("exist");
                for (DataSnapshot singleSnapshot : ds.getChildren()) {
                    TripToCompany trpObj = (TripToCompany) singleSnapshot.getValue(TripToCompany.class);
                    ArrayList<EmpToCompany>ee= trpObj.getEmptoCompanyList();
                    System.out.println("Company Name: "+trpObj.getCompany()); //successfully retrived
                    System.out.println("Employee Count: " + ee.size()); //unable to fetch it. Gives NullPointerException

                }
            } else {
                System.out.println("error");
            }
        }

        @Override
        public void onCancelled(DatabaseError de) {}
    });

I am able to get the remaining data successfully from firebase.我能够从firebase成功获取剩余数据。 The data in red rectangle is the ArrayList, and I am unable to fetch it(show in red rectangle in the image).红色矩形中的数据是ArrayList,我无法获取它(在图像中以红色矩形显示)。 I have printed in the console using Sysout and I am unable to get ArrayList data.我已使用Sysout在控制台中打印,但无法获取 ArrayList 数据。 It returns NullPointerException .它返回NullPointerException How can I fetch that ArrayList in the TripToCompany Object?如何在TripToCompany对象中获取该 ArrayList?

To solve this, please change the following lines of code: 要解决此问题,请更改以下代码行:

public void setEmptoCompanyList(ArrayList<EmpToCompany> emptoCompanyList) {
    emptoCompanyList = emptoCompanyList;
}

to

public void setEmptoCompanyList(ArrayList<EmpToCompany> emptoCompanyList) {
    this.emptoCompanyList = emptoCompanyList;
//    ^
}

You should assign the value of the local emptoCompanyList variable to the member class ( this ) field, not to the same variable. 您应该将本地emptoCompanyList变量的值分配给成员类( this )字段,而不是同一变量。

In your JSON case from the picture I see, you need to change this code:在我看到的图片中的 JSON 案例中,您需要更改此代码:

ArrayList< EmpToCompany> emptoCompanyList; ArrayList<EmpToCompany> emptoCompanyList;

to:到:

ArrayList< String> emptoCompanyList; ArrayList<String> emptoCompanyList;

But if you want arraylist of EmpToCompany class, you need to show what you have inside the red rectangle for keys 0 and 1 .... and I will help you :)但是,如果您想要 EmpToCompany 类的数组列表,则需要显示键 0 和 1 的红色矩形内的内容......我会帮助您:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Firebase 检索 ArrayList - Retrieve an ArrayList from Firebase 从BroadcastReceiver中的Service获取arraylist会给出NullpointerException - Getting arraylist from Service in BroadcastReceiver gives NullpointerException 从 Firebase 为每个特定帖子检索特定数据比 datasnapshot 更好的方法? 返回的位置错误 - Better way than datasnapshot to retrieve specific data from Firebase for each specific post? Location returned is wrong 如何从 Firebase 检索数据? dataSnapshot 有对象,但 getValue() 将返回 null - How to retrieve data from Firebase? dataSnapshot has the object but getValue() will return null Android 从 Firebase 检索数据并将其保存在 ArrayList - Android retrieve the data from Firebase and save it in ArrayList 从firebase检索数据到arraylist的问题 - Problem in retrieve data from firebase to arraylist 为什么即使 Firebase 数据快照包含子项,我的 ArrayList 也是空的 - why is my ArrayList empty even though firebase datasnapshot contains children 数据快照未从 Firebase 返回正确的数据 - Datasnapshot not returning correct data from Firebase 如何从 firebase 数据快照中获取数据? - How to fetch data from firebase datasnapshot? Firebase DataSnapshot,无法检索正确的子项列表 - Firebase DataSnapshot, Can't retrieve the right list of children
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM