gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:35:25 25 4
gpt4 key购买 nike

我的 Firebase 数据库如下所示

Firebase Image

蓝色矩形是我要检索的数据。

蓝色矩形的模态类如下所示

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);

}
}

我正在使用标准查询从 firebase 获取数据。这是代码

    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) {}
});

我能够从 firebase 成功获取剩余数据。红色矩形中的数据是ArrayList,我无法获取它(如图中的红色矩形所示)。我已使用 Sysout 在控制台中打印,但无法获取 ArrayList 数据。它返回NullPointerException。如何获取 TripToCompany 对象中的 ArrayList?

最佳答案

要解决此问题,请更改以下代码行:

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

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

您应该将本地 emptoCompanyList 变量的值分配给成员类 (this) 字段,而不是同一变量。

关于java - Firebase 从 DataSnapShot 检索存储的 ArrayList 给出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53322567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com