Firebase retrieve stored ArrayList from DataSnapShot gives NullPointerException










0















My firebase database looks likes this



Firebase Image



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?










share|improve this question
























  • What does this line of code System.out.println("Employee Count: " + ee.size()); return?

    – Alex Mamo
    Nov 15 '18 at 16:25











  • @AlexMamo it gives null pointer exception

    – Omsagar Gupta
    Nov 15 '18 at 16:36















0















My firebase database looks likes this



Firebase Image



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?










share|improve this question
























  • What does this line of code System.out.println("Employee Count: " + ee.size()); return?

    – Alex Mamo
    Nov 15 '18 at 16:25











  • @AlexMamo it gives null pointer exception

    – Omsagar Gupta
    Nov 15 '18 at 16:36













0












0








0








My firebase database looks likes this



Firebase Image



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?










share|improve this question
















My firebase database looks likes this



Firebase Image



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?







java android firebase firebase-realtime-database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 16:38







Omsagar Gupta

















asked Nov 15 '18 at 15:19









Omsagar GuptaOmsagar Gupta

387




387












  • What does this line of code System.out.println("Employee Count: " + ee.size()); return?

    – Alex Mamo
    Nov 15 '18 at 16:25











  • @AlexMamo it gives null pointer exception

    – Omsagar Gupta
    Nov 15 '18 at 16:36

















  • What does this line of code System.out.println("Employee Count: " + ee.size()); return?

    – Alex Mamo
    Nov 15 '18 at 16:25











  • @AlexMamo it gives null pointer exception

    – Omsagar Gupta
    Nov 15 '18 at 16:36
















What does this line of code System.out.println("Employee Count: " + ee.size()); return?

– Alex Mamo
Nov 15 '18 at 16:25





What does this line of code System.out.println("Employee Count: " + ee.size()); return?

– Alex Mamo
Nov 15 '18 at 16:25













@AlexMamo it gives null pointer exception

– Omsagar Gupta
Nov 15 '18 at 16:36





@AlexMamo it gives null pointer exception

– Omsagar Gupta
Nov 15 '18 at 16:36












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer























  • Hi Omsagar! Is there everything alright, have you solved the issue?

    – Alex Mamo
    Nov 16 '18 at 8:38











  • Thanks a lot. It worked :)

    – Omsagar Gupta
    Nov 19 '18 at 16:37











  • Good to hear that :) You're welcome, cheers!

    – Alex Mamo
    Nov 19 '18 at 16:45










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322567%2ffirebase-retrieve-stored-arraylist-from-datasnapshot-gives-nullpointerexception%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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.






share|improve this answer























  • Hi Omsagar! Is there everything alright, have you solved the issue?

    – Alex Mamo
    Nov 16 '18 at 8:38











  • Thanks a lot. It worked :)

    – Omsagar Gupta
    Nov 19 '18 at 16:37











  • Good to hear that :) You're welcome, cheers!

    – Alex Mamo
    Nov 19 '18 at 16:45















1














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.






share|improve this answer























  • Hi Omsagar! Is there everything alright, have you solved the issue?

    – Alex Mamo
    Nov 16 '18 at 8:38











  • Thanks a lot. It worked :)

    – Omsagar Gupta
    Nov 19 '18 at 16:37











  • Good to hear that :) You're welcome, cheers!

    – Alex Mamo
    Nov 19 '18 at 16:45













1












1








1







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 16:43









Alex MamoAlex Mamo

46k82965




46k82965












  • Hi Omsagar! Is there everything alright, have you solved the issue?

    – Alex Mamo
    Nov 16 '18 at 8:38











  • Thanks a lot. It worked :)

    – Omsagar Gupta
    Nov 19 '18 at 16:37











  • Good to hear that :) You're welcome, cheers!

    – Alex Mamo
    Nov 19 '18 at 16:45

















  • Hi Omsagar! Is there everything alright, have you solved the issue?

    – Alex Mamo
    Nov 16 '18 at 8:38











  • Thanks a lot. It worked :)

    – Omsagar Gupta
    Nov 19 '18 at 16:37











  • Good to hear that :) You're welcome, cheers!

    – Alex Mamo
    Nov 19 '18 at 16:45
















Hi Omsagar! Is there everything alright, have you solved the issue?

– Alex Mamo
Nov 16 '18 at 8:38





Hi Omsagar! Is there everything alright, have you solved the issue?

– Alex Mamo
Nov 16 '18 at 8:38













Thanks a lot. It worked :)

– Omsagar Gupta
Nov 19 '18 at 16:37





Thanks a lot. It worked :)

– Omsagar Gupta
Nov 19 '18 at 16:37













Good to hear that :) You're welcome, cheers!

– Alex Mamo
Nov 19 '18 at 16:45





Good to hear that :) You're welcome, cheers!

– Alex Mamo
Nov 19 '18 at 16:45



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322567%2ffirebase-retrieve-stored-arraylist-from-datasnapshot-gives-nullpointerexception%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3