I've been working on a new app that uses firebase database with a couple of Booleans stored inside. important_job and washed
"TEST" : {
"TEST" : {
"Current_job" : {
"important_job" : true,
"washed" : true
},
"colour" : "test",
"date" : "test",
"doors" : "test",
"enginesize" : "test",
"fuel" : "test",
"lotnumber" : "test",
"manufacturer" : "test",
"model" : "test",
"reg" : "TEST"
}
I'm trying to call these Booleans in my app by using this method:
public Harrop_JobListener(View relativeLayout) {
this.relativeLayout = relativeLayout;
}
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot valueSnapshot: dataSnapshot.getChildren()){
a = valueSnapshot.child("important_job").getValue(Boolean.class)
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
}
Then I use the public Boolean in my checkbox class like this:
harrop_jobListener = new Harrop_JobListener(myLayout);
dbref.addValueEventListener(harrop_jobListener);
cb1 = (CheckBox) myLayout.findViewById(R.id.cbimportant);
cb1.setChecked(harrop_jobListener.a);
cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (cb1.isChecked()) {
dbref.child("important_job").setValue(true);
} else {
dbref.child("important_job").setValue(false);
}
}
});
but for some reason I keep getting this error in my logcat:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference
at studios.p9p.harrop99.provaletercompanion.Checkbox_Helper.getCheckBoxes(Checkbox_Helper.java:54)
at studios.p9p.harrop99.provaletercompanion.HarropChildListener$1.onItemClick(HarropChildListener.java:100)
I've checked to see that I'm getting the right key and values using
System.out.println(valueSnapshot.getKey());
System.out.println(valueSnapshot.getValue(Boolean.class))
which returns the correct results. So I'm left confused as to what I'm doing wrong again.
Edit
This is my dbref
dbref = db.getReference().child(registration).child(registration).child("Current_job");
the registration child = whatever reg the car the user will be working on at the time.