1

so I have written this code that reads an item from my data base and writes the information to various text views. what I now want to do is delete the item referenced as 'result' from the database once it has written to the text views.
please note that all this code works as intended however once the text views are populated with the data and the item is deleted from the database the app stops(crashes).

DatabaseReference newRef = FirebaseDatabase.getInstance().getReference("messages").child(result);
            newRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists()) {
                        Log.e("Count", "" + dataSnapshot.getChildren());
                        String User = (String) d  ataSnapshot.child("name").getValue();
                        String TheMessage = (String) dataSnapshot.child("message").getValue();
                        String Email = (String) dataSnapshot.child("email").getValue();
                        //check for email match
                        FirebaseUser curuser = FirebaseAuth.getInstance().getCurrentUser();
                        if (!Email.equals(curuser.getEmail())) {

                            //i try to remove the value here
                            myRef.child(result).removeValue();

                            message.setText(themessage);
                            from.setText("from");
                            user.setText(User);
                        } 
                    }
                    }
                    @Override
                    public void onCancelled (DatabaseError databaseError){
                        Log.e(TAG, "The read failed");
                    }

I have done extensive research into why this is happening and on other threads it recommends that I add if (datasnapshot.exsists()){//my code} then it would stop my app crashing but that hasn't worked.

my error message is:

    E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.fujdevelopers.bottled, PID: 7274
              java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
                  at com.fujdevelopers.bottled.readMsg$1.onDataChange(readMsg.java:52)
                  at com.google.android.gms.internal.zzbpx.zza(Unknown Source)
                  at com.google.android.gms.internal.zzbqx.zzZS(Unknown Source)
                  at com.google.android.gms.internal.zzbra$1.run(Unknown Source)
                  at android.os.Handler.handleCallback(Handler.java:746)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5443)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

if I take out the line:

     myRef.child(result).removeValue();

then it all works without crashing but if i leave it in it all work but crashes.

If anyone can help me out i would very much appreciate it.

EDIT: I know what a null pointer is and i understand why they happen i just don't get why i have one in this context.(Im new to firebase databases).

also

the database reference shown in my code is

DatabaseReference newRef = FirebaseDatabase.getInstance().getReference("messages").child(result);

however the delete uses reference "myRef" which is this

DatabaseReference myRef = FirebaseDatabase.getInstance().getReference("messages");

I cant understand why after all the code is executed i get the null pointer and my program crashes

EDIT I fixed it, I added a remove event listener.

  • 3
    duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – petey Feb 23 '17 at 00:50

1 Answers1

0

I would like to give suggestion don't know if it will help.

According to your given error. It seem like you trying to get the data from the item that is deleted and somehow it got NullPointerException.

Please try to add the listener to FirebaseDatabase.getInstance().getReference("messages") instead of FirebaseDatabase.getInstance().getReference("messages").child(result) then loop through the dataSnapshot to the item that has a key equal to result then check and perform the deletion there.

Boonya Kitpitak
  • 3,607
  • 1
  • 30
  • 30
  • i should elaborate if you look at the code newRef references the child. But it deletes from myRef which references "message", so my current code does what you say it should do, sorry for not being clear in my post – constant vigilance Feb 23 '17 at 13:43
  • I didn't see any looping in your `onDataChange`. But it seems like you manage to solve your problem. Congrats! – Boonya Kitpitak Feb 24 '17 at 04:10