Skip to content

Commit 215ff7c

Browse files
committed
Fix potential NPE in user fetch
Bug: 28634722 Change-Id: Ief3938e4baf0d499e4a2975b2e6b7990dee67921
1 parent f71c69b commit 215ff7c

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

database/app/src/main/java/com/google/firebase/quickstart/database/NewPostActivity.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.util.Log;
66
import android.view.View;
77
import android.widget.EditText;
8+
import android.widget.Toast;
89

910
import com.google.firebase.database.DataSnapshot;
1011
import com.google.firebase.database.DatabaseError;
@@ -71,8 +72,16 @@ public void onDataChange(DataSnapshot dataSnapshot) {
7172
User user = dataSnapshot.getValue(User.class);
7273

7374
// [START_EXCLUDE]
74-
// Write new post
75-
writeNewPost(userId, user.username, title, body);
75+
if (user == null) {
76+
// User is null, error out
77+
Log.e(TAG, "User " + userId + " is unexpectedly null");
78+
Toast.makeText(NewPostActivity.this,
79+
"Error: could not fetch user.",
80+
Toast.LENGTH_SHORT).show();
81+
} else {
82+
// Write new post
83+
writeNewPost(userId, user.username, title, body);
84+
}
7685

7786
// Finish this Activity, back to the stream
7887
finish();

0 commit comments

Comments
 (0)