-2
@Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            if (snapshot.hasChildren()) {
                for (DataSnapshot mDataSnapshot : snapshot.getChildren()) {
                    String suhu = mDataSnapshot.child("Suhu").getValue().toString();
                    Suhuu.setText(suhu);
                    final Float nilai = Float.valueOf(suhu);
                     if (nilai >= 22 && nilai <= 33) {
                        kondisi.setText("Ideal");
                    } else if (nilai >= 34 && nilai <= 59) {
                        kondisi.setText("Panas");
                    } else if (nilai < 22) {
                        kondisi.setText("Dingin");
                    } else if (nilai > 60) {
                        kondisi.setText("Sangat Panas");
                    }
                }
            }

        }

i have a problem when i run my code, i want to fetch data from firebase and show graphic to android studio. How to handle it? ` enter image description here

1 Answers1

0

The diagnostic is telling you that you are attempting to invoke toString() on the value null. If the code presented is to blame, then that can only be arising from mDataSnapshot.child("Suhu").getValue() returning null. You need to accommodate that possibility one way or another.

One alternative would be to use String.valueOf(Object) instead of Object.toString(). That is:

String suhu = String.valueOf(mDataSnapshot.child("Suhu").getValue());

That will produce "null" when getValue() returns null, and otherwise will produce the result of invoking toString() on the value.

I am inclined to guess, however, that you also want to look into why getValue() is returning null. You don't seem to have expected that possibility, and it might be a sign that something is going wrong elsewhere in your program. At minimum, you should come to an understanding of why that happens in order to be confident that it is correct.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157