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.