-1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
markharrop
  • 866
  • 1
  • 9
  • 30
  • ive added it to my question @PeterHaddad – markharrop Apr 01 '18 at 23:16
  • 1
    Sorry, but there are too many disparate snippets here to follow the flow. If you need help, the best way is to create a [minimal, complete, verifiable example](http://stackoverflow.com/help/mcve). But in this case, the problem should be easy to spot if you look at `Checkbox_Helper.java:54`. For more on how to troubleshoot `NullPointerException`s, see https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Frank van Puffelen Apr 02 '18 at 01:44
  • thanks @FrankvanPuffelen took me a while but i think ive sussed it. ive removed the for statement and all works great, thanks for your help today and yesterday. i know my code and questions can be messy sometimes but that because ive taught my self. if you saw my code a year ago you would know it has improved a great deal lol – markharrop Apr 02 '18 at 03:05
  • 2
    Good to hear that you figured it out mark! And great to hear about the progress. Just keep in mind that the best way to ask for help is to isolate the problem. While this takes more effort on your part, it both helps you to reason better about your code, and increases the chance that we can help you efficiently. – Frank van Puffelen Apr 02 '18 at 04:08
  • thanks pal and thanks for all you do for stack overflow as a whole – markharrop Apr 02 '18 at 13:10

1 Answers1

1

If I understand correctly, you should set this db child's default value to a boolean, as from your problem, it seems that when checking for updates to the DB, it does not retrieve the new values in time. To prevent the error from happening, please set this default value and tell us if it continues. A similar ordeal used to occur on my app.

Harris B
  • 113
  • 3
  • 7
  • so i should set the value manually first to somethng like false? – markharrop Apr 01 '18 at 23:34
  • that fixes there error but only because it read the boolean value from the default and not from the database. – markharrop Apr 02 '18 at 00:16
  • Theoretically, it should read the data whenever it changes due to it being a listener, so if you can get it to set the "a" variable to the DB value the second time around, that might give you your answer – Harris B Apr 02 '18 at 01:37