【问题标题】:Firebase DatabaseReference null pointer exceptionFirebase DatabaseReference 空指针异常
【发布时间】:2026-02-20 05:30:01
【问题描述】:

我刚刚创建了一个带有四个 EditText 和一个按钮的 Hello World 应用程序。该应用程序因以下错误而崩溃: 我在按钮单击事件上遇到空指针异常。

学生信息类:

class StudentInformation {
private String name, rollNo, courseName, marks;
StudentInformation(String nameTxt, String rollNoTxt, String courseNameTxt, String marksTxt){
    this.name = nameTxt;
    this.rollNo = rollNoTxt;
    this.courseName = courseNameTxt;
    this.marks = marksTxt;
}

StudentInformation(){
    this.name = "";
    this.rollNo = "";
    this.courseName = "";
    this.marks = "";

}


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getRollNo() {
    return rollNo;
}

public void setRollNo(String rollNo) {
    this.rollNo = rollNo;
}

public String getCourseName() {
    return courseName;
}

public void setCourseName(String courseName) {
    this.courseName = courseName;
}

public String getMarks() {
    return marks;
}

public void setMarks(String marks) {
    this.marks = marks;
}

}

我的代码是:

public class MainActivity extends AppCompatActivity {
    FirebaseDatabase mDatabase;
    DatabaseReference mDatabaseReference;
    StudentInformation studentInformation;
    EditText name, rollNo, course, marks;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    Button saveToDatabase = findViewById(R.id.button);

    name = findViewById(R.id.nameText);
    rollNo = findViewById(R.id.rollNoText);
    course = findViewById(R.id.courseNameText);
    marks = findViewById(R.id.marksText);

    mDatabase = FirebaseDatabase.getInstance();
    mDatabaseReference = mDatabase.getReference().child("user");

    saveToDatabase.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            studentInformation = new StudentInformation(name.getText().toString(), rollNo.getText().toString(), course.getText().toString(), marks.getText().toString());
            mDatabaseReference.setValue(studentInformation);


     Toast.makeText(MainActivity.this, "Data saved", Toast.LENGTH_SHORT).show();

//                Intent myIntent = new Intent(MainActivity.this, SecondActivity.class);
//                startActivity(myIntent);
            }
        });

    }
}

编辑:正如所指出的,我使用了错误的 ID。它在使用 R.id.button 而不是 R.id.bottom 后得到纠正,但现在我在网上获得了另一个空引用:

mDatabaseReference = mDatabase.getReference().child("user");

由于我的上一期和这一期也与空指针异常有关,这就是为什么我没有为此创建一个新问题。

任何人都可以帮忙。我相信如果 Firebase 中不存在标签“用户”,Firebase 不应该自己创建它吗?

确切的错误是:

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.DatabaseReference com.google.firebase.database.FirebaseDatabase.getReference()' on a null object reference
    at com.smd.firebasequiz.MainActivity$1.onClick(MainActivity.java:34)

Firebase 屏幕截图:

【问题讨论】:

  • 检查按钮 id 是否为您在 XML 中设置的 bottom
  • 请分享您的 xml 文件。
  • 我解决了这个问题。显然我使用了“底部”而不是“按钮”。你能帮忙解决一下 DatabaseReference 上的 NPE 问题吗?

标签: android firebase firebase-realtime-database onclicklistener


【解决方案1】:

FirebaseDatabase mDatabase;这里mDatabase等于null,所以在调用FirebaseDatabase类中的任何方法之前需要先初始化:

mDatabase = FirebaseDatabase.getInstance();
mDatabaseReference = mDatabase.getReference().child("user");

【讨论】:

  • 好眼光。这不是他最初错误的答案,它解决了另一个可能的错误来源。不确定他在哪里初始化他的mDatabase。它可能在onStart() 中,这不会在onClickListener() 中产生NullPointerException,写在onCreate() 中。
  • 我怀疑他是否在任何地方初始化它.. 但是是的,它似乎完全改变了问题......
  • 我添加了整个活动代码。这防止了由于 NPE 导致的崩溃,但数据没有保存到 Firebase。我的 Firebase 还是空的 :(
【解决方案2】:

尝试更改 DatabaseReference 对象的访问修饰符。将其设为默认值(只需删除单词 private)。另外,为 StudentInformation 类创建一个空的默认构造函数。

【讨论】:

  • 我按照你说的更新了代码。请看一下。仍然在按钮单击事件中,我正在执行 Toast 指示按钮,但没有任何内容上传到 Firebase