1

I am trying to write data with setValue() to firebase database, but it throws NullPointerException. I have no idea what can cause NullPointerException with this method, so i am asking for help. Here is the error:

Exception in thread "Thread-24" java.lang.NullPointerException
    at com.example.maxgm.myapplication.backend.EnergyBar$1$2.onComplete(EnergyBar.java:81)
    at com.google.firebase.database.core.Repo$6.run(Repo.java:329)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at com.google.appengine.tools.development.BackgroundThreadFactory$1$1.run(BackgroundThreadFactory.java:60)

Here is my code:

@Override
public void onDataChange(DataSnapshot snapshot)
{
    if (snapshot.exists())
    {
        for (DataSnapshot childSnap : snapshot.getChildren()) {
            if (childSnap.child("Character").exists()) {
                int energyCurrent;
                int energyMax;
                int energyObtain;
                energyCurrent = (int) (long) childSnap.child("Character").child("energyCurrent").getValue();
                energyMax = (int) (long) childSnap.child("Character").child("energyMax").getValue();
                energyObtain = (int) (long) childSnap.child("Character").child("energyObtain").getValue();

                float finalEnergy = energyCurrent + (energyMax * (energyObtain / 100));
                if (finalEnergy > energyMax) {
                    childSnap.getRef().child("Character").child("energyCurrent").setValue(energyMax, new DatabaseReference.CompletionListener() {
                        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                            throw databaseError.toException();
                        }
                    });
                } else {
                    childSnap.getRef().child("Character").child("energyCurrent").setValue((int) finalEnergy, new DatabaseReference.CompletionListener() {
                        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                            throw databaseError.toException();
                        }
                    });
                }
            }
        }
    }
}
Maxgmer
  • 456
  • 8
  • 19
  • 1
    Darn. Skeet beat me to it. But the answer is that you're failing to handle the case where the write succeeded. In that case there is no error, so `databaseError` will be `null`. – Frank van Puffelen Dec 16 '16 at 16:11
  • @FrankvanPuffelen how to handle the case where the write succeeded? – Maxgmer Dec 16 '16 at 16:17
  • 1
    That depends solely on what you want to do when the write succeeds. But to get rid of the error, `if (databaseError != null) { throw databaseError.toException(); }`. The link Jon provided should explain that in detail. – Frank van Puffelen Dec 16 '16 at 17:44
  • @FrankvanPuffelen oh, i think i ve got it, trowing exception from dbError causes exception, right? xD Thanks very much!) – Maxgmer Dec 19 '16 at 07:09
  • @FrankvanPuffelen but now no exceptions sent and values in database dont change(( wtf is happening with my shit code. – Maxgmer Dec 19 '16 at 07:23

0 Answers0