This is how my data looks like:
{
"courses":{
"Business":{
"PushKey_1":{
"courseC":"B1111",
"name":"Business Year 1"
},
"PushKey_2":{
"courseC":"B2222",
"name":"Business Year 2"
},
}
}
}
I'm trying to retrieve the push key based on a child. Code I'm trying now:
String testCourseC = "B2222";
String showCourseKey;
DatabaseReference mRef = FirebaseDatabase.getInstance().getReference().child("courses").child("Business");
mRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()){
CourseDetails data = ds.getValue(CourseDetails.class);
String compareC = data.getCourseC();
if (compareC.equals(testCourseC)){
showCourseKey = dataSnapshot.getKey();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
textview1.setText(showCourseKey);
My CourseDetails
class:
public class CourseDetails {
private String courseC;
private String name;
public CourseDetails() {
}
public CourseDetails(String courseC, String name) {
this.courseC = courseC;
this.name = name;
}
public String getCourseC() {
return courseC;
}
public void setCourseC(String courseC) {
this.courseC = courseC;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
textview1 should display PushKey_2. If the code was wrong, shouldn't textview1 display "courses"?
I got NullPointerException from the code above. I have read the documentation and gone through similar questions, but still got this wrong. Please don't mind my silly question
I run your code and do not get a
NullPointerException
. Please post the stack trace.I will point out what looks like an error.
This:
should be: