How to get child object's value inside a key from a list of data (using childeventlistener in firebase:...











up vote
1
down vote

favorite












This is the Firebase JSON structure



Ok so I've been looking for related articles regarding this, I've made a few experiments but I can't understand why I can't still get the values of note, date_time and vaccine objects... I'm planning on putting them in a ListView and I already got the key from the list of data using ChildEventListener



 lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");

lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String string = dataSnapshot.getValue(String.class);


}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});


and then I've tried using EventListener to get the values inside of it



 lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");

lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String string = dataSnapshot.getValue(String.class);

DatabaseReference newRef = lastlastref.child(string);

newRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
note = snapshot.child("note").getValue(String.class);
vaccine = snapshot.child("vaccine").getValue(String.class);
timestamp = snapshot.child("date_time").getValue(String.class);

}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
arrayList.add(vaccine + "" + timestamp + "" + note);
adapter.notifyDataSetChanged();

}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});


But the app still crashes and logcat says "Can't pass null for argument 'pathString' in child()"










share|improve this question
























  • This means that babyid is null, which is not allowed in calls to child(). You can easily verify the value of babyid by running the code in a debugger, and putting a breakpoint on the line with lastlastref = myRef.child(babyid)....
    – Frank van Puffelen
    Nov 20 at 4:34










  • ok, fixed the null error but it still won't get the children object values... could my code be lacking something?
    – Jackie Chan
    Nov 20 at 16:03















up vote
1
down vote

favorite












This is the Firebase JSON structure



Ok so I've been looking for related articles regarding this, I've made a few experiments but I can't understand why I can't still get the values of note, date_time and vaccine objects... I'm planning on putting them in a ListView and I already got the key from the list of data using ChildEventListener



 lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");

lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String string = dataSnapshot.getValue(String.class);


}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});


and then I've tried using EventListener to get the values inside of it



 lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");

lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String string = dataSnapshot.getValue(String.class);

DatabaseReference newRef = lastlastref.child(string);

newRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
note = snapshot.child("note").getValue(String.class);
vaccine = snapshot.child("vaccine").getValue(String.class);
timestamp = snapshot.child("date_time").getValue(String.class);

}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
arrayList.add(vaccine + "" + timestamp + "" + note);
adapter.notifyDataSetChanged();

}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});


But the app still crashes and logcat says "Can't pass null for argument 'pathString' in child()"










share|improve this question
























  • This means that babyid is null, which is not allowed in calls to child(). You can easily verify the value of babyid by running the code in a debugger, and putting a breakpoint on the line with lastlastref = myRef.child(babyid)....
    – Frank van Puffelen
    Nov 20 at 4:34










  • ok, fixed the null error but it still won't get the children object values... could my code be lacking something?
    – Jackie Chan
    Nov 20 at 16:03













up vote
1
down vote

favorite









up vote
1
down vote

favorite











This is the Firebase JSON structure



Ok so I've been looking for related articles regarding this, I've made a few experiments but I can't understand why I can't still get the values of note, date_time and vaccine objects... I'm planning on putting them in a ListView and I already got the key from the list of data using ChildEventListener



 lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");

lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String string = dataSnapshot.getValue(String.class);


}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});


and then I've tried using EventListener to get the values inside of it



 lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");

lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String string = dataSnapshot.getValue(String.class);

DatabaseReference newRef = lastlastref.child(string);

newRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
note = snapshot.child("note").getValue(String.class);
vaccine = snapshot.child("vaccine").getValue(String.class);
timestamp = snapshot.child("date_time").getValue(String.class);

}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
arrayList.add(vaccine + "" + timestamp + "" + note);
adapter.notifyDataSetChanged();

}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});


But the app still crashes and logcat says "Can't pass null for argument 'pathString' in child()"










share|improve this question















This is the Firebase JSON structure



Ok so I've been looking for related articles regarding this, I've made a few experiments but I can't understand why I can't still get the values of note, date_time and vaccine objects... I'm planning on putting them in a ListView and I already got the key from the list of data using ChildEventListener



 lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");

lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String string = dataSnapshot.getValue(String.class);


}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});


and then I've tried using EventListener to get the values inside of it



 lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");

lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String string = dataSnapshot.getValue(String.class);

DatabaseReference newRef = lastlastref.child(string);

newRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
note = snapshot.child("note").getValue(String.class);
vaccine = snapshot.child("vaccine").getValue(String.class);
timestamp = snapshot.child("date_time").getValue(String.class);

}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
arrayList.add(vaccine + "" + timestamp + "" + note);
adapter.notifyDataSetChanged();

}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});


But the app still crashes and logcat says "Can't pass null for argument 'pathString' in child()"







android firebase firebase-realtime-database






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 17:29









Frank van Puffelen

223k26364390




223k26364390










asked Nov 20 at 4:18









Jackie Chan

104




104












  • This means that babyid is null, which is not allowed in calls to child(). You can easily verify the value of babyid by running the code in a debugger, and putting a breakpoint on the line with lastlastref = myRef.child(babyid)....
    – Frank van Puffelen
    Nov 20 at 4:34










  • ok, fixed the null error but it still won't get the children object values... could my code be lacking something?
    – Jackie Chan
    Nov 20 at 16:03


















  • This means that babyid is null, which is not allowed in calls to child(). You can easily verify the value of babyid by running the code in a debugger, and putting a breakpoint on the line with lastlastref = myRef.child(babyid)....
    – Frank van Puffelen
    Nov 20 at 4:34










  • ok, fixed the null error but it still won't get the children object values... could my code be lacking something?
    – Jackie Chan
    Nov 20 at 16:03
















This means that babyid is null, which is not allowed in calls to child(). You can easily verify the value of babyid by running the code in a debugger, and putting a breakpoint on the line with lastlastref = myRef.child(babyid)....
– Frank van Puffelen
Nov 20 at 4:34




This means that babyid is null, which is not allowed in calls to child(). You can easily verify the value of babyid by running the code in a debugger, and putting a breakpoint on the line with lastlastref = myRef.child(babyid)....
– Frank van Puffelen
Nov 20 at 4:34












ok, fixed the null error but it still won't get the children object values... could my code be lacking something?
– Jackie Chan
Nov 20 at 16:03




ok, fixed the null error but it still won't get the children object values... could my code be lacking something?
– Jackie Chan
Nov 20 at 16:03












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










You set up your reference to:



lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");


So each child node under this location is a JSON object like this:



{
"date_time": "November/16/2018...",
"note": "hhjj...",
"vaccine": "Measles"
}


But in your onChildAdded, you're trying to retrieve a single string value. Since the above JSON object is not a single string value, the getValue(String.class) returns null.



To get the values, you can call getValue() on the individual properties:



lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String date_time = dataSnapshot.child("date_time").getValue(String.class);
String note = dataSnapshot.child("note").getValue(String.class);
String vaccine = dataSnapshot.child("vaccine").getValue(String.class);
}


You can also create a minimal class to wrap each record, and read that. The simplest version of that is:



public class ImmunizationRecord {
public String date_time;
public String note;
public String vaccine;
}


And the reading would then be done with:



public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String record = dataSnapshot.getValue(ImmunizationRecord.class);
}




A small side note about the way you store date_time. This format will lead to problems as you progress, since it is not sortable. If you need to store time stamps, store them as milliseconds since the epoch, or in a string format that allows them to be sorted (e.g. 2018-11-16T11:29:00).






share|improve this answer





















  • Worked like a charm. Thanks a lot!
    – Jackie Chan
    Nov 21 at 9:28











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',
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%2f53386147%2fhow-to-get-child-objects-value-inside-a-key-from-a-list-of-data-using-childeve%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








up vote
0
down vote



accepted










You set up your reference to:



lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");


So each child node under this location is a JSON object like this:



{
"date_time": "November/16/2018...",
"note": "hhjj...",
"vaccine": "Measles"
}


But in your onChildAdded, you're trying to retrieve a single string value. Since the above JSON object is not a single string value, the getValue(String.class) returns null.



To get the values, you can call getValue() on the individual properties:



lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String date_time = dataSnapshot.child("date_time").getValue(String.class);
String note = dataSnapshot.child("note").getValue(String.class);
String vaccine = dataSnapshot.child("vaccine").getValue(String.class);
}


You can also create a minimal class to wrap each record, and read that. The simplest version of that is:



public class ImmunizationRecord {
public String date_time;
public String note;
public String vaccine;
}


And the reading would then be done with:



public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String record = dataSnapshot.getValue(ImmunizationRecord.class);
}




A small side note about the way you store date_time. This format will lead to problems as you progress, since it is not sortable. If you need to store time stamps, store them as milliseconds since the epoch, or in a string format that allows them to be sorted (e.g. 2018-11-16T11:29:00).






share|improve this answer





















  • Worked like a charm. Thanks a lot!
    – Jackie Chan
    Nov 21 at 9:28















up vote
0
down vote



accepted










You set up your reference to:



lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");


So each child node under this location is a JSON object like this:



{
"date_time": "November/16/2018...",
"note": "hhjj...",
"vaccine": "Measles"
}


But in your onChildAdded, you're trying to retrieve a single string value. Since the above JSON object is not a single string value, the getValue(String.class) returns null.



To get the values, you can call getValue() on the individual properties:



lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String date_time = dataSnapshot.child("date_time").getValue(String.class);
String note = dataSnapshot.child("note").getValue(String.class);
String vaccine = dataSnapshot.child("vaccine").getValue(String.class);
}


You can also create a minimal class to wrap each record, and read that. The simplest version of that is:



public class ImmunizationRecord {
public String date_time;
public String note;
public String vaccine;
}


And the reading would then be done with:



public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String record = dataSnapshot.getValue(ImmunizationRecord.class);
}




A small side note about the way you store date_time. This format will lead to problems as you progress, since it is not sortable. If you need to store time stamps, store them as milliseconds since the epoch, or in a string format that allows them to be sorted (e.g. 2018-11-16T11:29:00).






share|improve this answer





















  • Worked like a charm. Thanks a lot!
    – Jackie Chan
    Nov 21 at 9:28













up vote
0
down vote



accepted







up vote
0
down vote



accepted






You set up your reference to:



lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");


So each child node under this location is a JSON object like this:



{
"date_time": "November/16/2018...",
"note": "hhjj...",
"vaccine": "Measles"
}


But in your onChildAdded, you're trying to retrieve a single string value. Since the above JSON object is not a single string value, the getValue(String.class) returns null.



To get the values, you can call getValue() on the individual properties:



lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String date_time = dataSnapshot.child("date_time").getValue(String.class);
String note = dataSnapshot.child("note").getValue(String.class);
String vaccine = dataSnapshot.child("vaccine").getValue(String.class);
}


You can also create a minimal class to wrap each record, and read that. The simplest version of that is:



public class ImmunizationRecord {
public String date_time;
public String note;
public String vaccine;
}


And the reading would then be done with:



public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String record = dataSnapshot.getValue(ImmunizationRecord.class);
}




A small side note about the way you store date_time. This format will lead to problems as you progress, since it is not sortable. If you need to store time stamps, store them as milliseconds since the epoch, or in a string format that allows them to be sorted (e.g. 2018-11-16T11:29:00).






share|improve this answer












You set up your reference to:



lastlastref = myRef.child(babyid).child("baby_features").child("immunization_records");


So each child node under this location is a JSON object like this:



{
"date_time": "November/16/2018...",
"note": "hhjj...",
"vaccine": "Measles"
}


But in your onChildAdded, you're trying to retrieve a single string value. Since the above JSON object is not a single string value, the getValue(String.class) returns null.



To get the values, you can call getValue() on the individual properties:



lastlastref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String date_time = dataSnapshot.child("date_time").getValue(String.class);
String note = dataSnapshot.child("note").getValue(String.class);
String vaccine = dataSnapshot.child("vaccine").getValue(String.class);
}


You can also create a minimal class to wrap each record, and read that. The simplest version of that is:



public class ImmunizationRecord {
public String date_time;
public String note;
public String vaccine;
}


And the reading would then be done with:



public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String record = dataSnapshot.getValue(ImmunizationRecord.class);
}




A small side note about the way you store date_time. This format will lead to problems as you progress, since it is not sortable. If you need to store time stamps, store them as milliseconds since the epoch, or in a string format that allows them to be sorted (e.g. 2018-11-16T11:29:00).







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 17:36









Frank van Puffelen

223k26364390




223k26364390












  • Worked like a charm. Thanks a lot!
    – Jackie Chan
    Nov 21 at 9:28


















  • Worked like a charm. Thanks a lot!
    – Jackie Chan
    Nov 21 at 9:28
















Worked like a charm. Thanks a lot!
– Jackie Chan
Nov 21 at 9:28




Worked like a charm. Thanks a lot!
– Jackie Chan
Nov 21 at 9:28


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53386147%2fhow-to-get-child-objects-value-inside-a-key-from-a-list-of-data-using-childeve%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







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen