My firebase database looks likes this
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. 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. The data in red rectangle is the ArrayList, and I am unable to fetch it(show in red rectangle in the image). I have printed in the console using Sysout and I am unable to get ArrayList data. It returns NullPointerException . How can I fetch that ArrayList in the TripToCompany Object?
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.
In your JSON case from the picture I see, you need to change this code:
ArrayList< EmpToCompany> emptoCompanyList;
to:
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 :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.