You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PutExtra后调用GetStringExtra仍返回Null?

Fixing the Null Pointer Error in Your Firebase Write Operation

Hey Anne, let's walk through the most common reasons that line of code is throwing a null pointer, and how to fix them step by step!

1. Check if mFirebaseDatabase is properly initialized

This is the #1 culprit for null pointers in Firebase setup. If you haven't initialized your database instance before calling child(), mFirebaseDatabase will be null.

Make sure you have this code before your problematic line, usually in your Activity's onCreate() method:

// Initialize Firebase Database
mFirebaseDatabase = FirebaseDatabase.getInstance();
// Optional: Enable offline persistence if needed
// mFirebaseDatabase.setPersistenceEnabled(true);

Double-check that you're not misspelling the variable name (typos happen to everyone!) and that the initialization runs before you try to write to the database.

2. Verify userKey isn't null (from the Intent Extra)

Since you're passing userKey via an Intent Extra, it's easy to mess up the key name or fail to retrieve it correctly.

First, confirm how you're sending the key from the first Activity:

// Example of sending the key
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("USER_KEY", yourActualUserKey); // Keep this key name consistent!
startActivity(intent);

Then check how you're retrieving it in the second Activity:

// Retrieve the key - match the EXACT key name (case-sensitive!)
String userKey = getIntent().getStringExtra("USER_KEY");

// Add a log to check if it's null
Log.d("IntentCheck", "Received userKey: " + userKey);

If the log shows userKey is null, double-check:

  • The key name matches exactly (no uppercase/lowercase mistakes)
  • The yourActualUserKey in the first Activity isn't null itself before sending

3. Ensure userNameString isn't null

While Firebase allows writing null values, if userNameString is null because you didn't properly fetch it (e.g., from an EditText), that could be the issue.

Check where userNameString comes from:

// Example if pulling from an EditText
EditText userNameInput = findViewById(R.id.user_name_input);
String userNameString = userNameInput.getText().toString().trim();

// Log this value too
Log.d("UserNameCheck", "userNameString: " + userNameString);

If the log shows null, make sure you've properly initialized the EditText (you called findViewById() correctly) and that the user hasn't left the field empty (though an empty string is different from null).

Quick Debugging Step

Before your problematic line, add these logs to pinpoint exactly which variable is null:

Log.d("FirebaseDebug", "mFirebaseDatabase: " + mFirebaseDatabase);
Log.d("FirebaseDebug", "userKey: " + userKey);
Log.d("FirebaseDebug", "userNameString: " + userNameString);

Run your app, check Logcat, and whichever line shows null is your starting point for fixing the issue.


内容的提问来源于stack exchange,提问作者Anne

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:37:38