Reputation: 148
I'm new to android and I've been playing around with this sample app from firebase. I want to add a delete button on PostDetailActivity which deletes the current post being viewed from the database by setting the DatabaseReference mPostReference to null. However this causes a null pointer exception to a ValueEventListener added to mPostReference, causing the app to crash.
// Get post key from intent
mPostKey = getIntent().getStringExtra(EXTRA_POST_KEY);
if (mPostKey == null) {
throw new IllegalArgumentException("Must pass EXTRA_POST_KEY");
}
// Initialize Database
mPostReference = FirebaseDatabase.getInstance().getReference()
.child("posts").child(mPostKey);
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
Post post = dataSnapshot.getValue(Post.class);
// Text Views in activity
mAuthorView.setText(post.author);
mTitleView.setText(post.title);
mBodyView.setText(post.body);
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// [START_EXCLUDE]
Toast.makeText(PostDetailActivity.this, "Failed to load post.",
Toast.LENGTH_SHORT).show();
// [END_EXCLUDE]
}
};
mPostReference.addValueEventListener(postListener);
//delete button clicked
deleteButton.setOnClickListener(new View.OnClickListener() {
mPostReference.setValue(null);
}
Is there a way to keep the app from crashing if mPostReference has been deleted or set to null? Are there better ways in implementing a delete button? Any ideas/suggestions would be greatly appreciated. Thank you
Here's the log
FATAL EXCEPTION: main
Process: com.google.firebase.quickstart.database, PID: 2861
java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.google.firebase.quickstart.database.models.Post.author' on a null object reference
at com.google.firebase.quickstart.database.PostDetailActivity$1.onDataChange(PostDetailActivity.java:93)
at com.google.android.gms.internal.zzbmz.zza(Unknown Source)
at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
at com.google.android.gms.internal.zzboc$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6145)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Upvotes: 1
Views: 1630
Reputation: 7720
You just need to add an if
statement to check if the dataSnapshot
has a value or null
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
// Get Post object and use the values to update the UI
Post post = dataSnapshot.getValue(Post.class);
// Text Views in activity
mAuthorView.setText(post.author);
mTitleView.setText(post.title);
mBodyView.setText(post.body);
}
}
Upvotes: 4