>我在尝试将数据添加到Firebase的实时数据库中时遇到空指针异常
package com.example.ecommerce;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.*;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.HashMap;
public class register extends AppCompatActivity {
EditText inputname, password, Phone;
Button createaccount;
ProgressDialog loadingbar;
FirebaseDatabase database;
DatabaseReference ref;
User user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
inputname = findViewById(R.id.username);
password = findViewById(R.id.reg_password);
Phone = findViewById(R.id.reg_phone_number);
createaccount = findViewById(R.id.create_account);
// loadingbar=new ProgressDialog(this);
ref = database.getReference("User");
user = new User();
}
public void getValues() {
user.setName(inputname.getText().toString());
user.setPhone(Phone.getText().toString());
user.setPassword(password.getText().toString());
}
public void btn(View view) {
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
getValues();
ref.child("user02").setValue(user);
Toast.makeText(register.this, "Data Inserted", Toast.LENGTH_LONG).show();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
用户类 :
package com.example.ecommerce;
public class User {
private String Name;
private String Phone;
private String Password;
public User() {
}
public User(String name, String phone, String password) {
Name = name;
Phone = phone;
Password = password;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getPhone() {
return Phone;
}
public void setPhone(String phone) {
Phone = phone;
}
public String getPassword() {
return Password;
}
public void setPassword(String password) {
Password = password;
}
}
.log:
2019-07-22 12:02:52.086 15273-15273/com.example.ecommerce E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ecommerce, PID: 15273
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ecommerce/com.example.ecommerce.register}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.DatabaseReference com.google.firebase.database.FirebaseDatabase.getReference(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3121)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:81)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1935)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7116)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:925)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.DatabaseReference com.google.firebase.database.FirebaseDatabase.getReference(java.lang.String)' on a null object reference
at com.example.ecommerce.register.onCreate(register.java:44)
at android.app.Activity.performCreate(Activity.java:7698)
at android.app.Activity.performCreate(Activity.java:7687)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3096)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:81)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1935)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7116)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:925)
您需要在 oncreate 方法中创建一个数据库对象。由于数据库对象,您得到空指针异常。您的oncreate方法代码应该是这样的。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
inputname = findViewById(R.id.username);
password = findViewById(R.id.reg_password);
Phone = findViewById(R.id.reg_phone_number);
createaccount = findViewById(R.id.create_account);
// loadingbar=new ProgressDialog(this);
database = FirebaseDatabase.getInstance();
ref = database.getReference("User");
user = new User();
}
database
为null 它不引用堆中的任何内容,您需要通过以下方式初始化它:
FirebaseDatabase database = FirebaseDatabase.getInstance();
然后你可以做:
DatabaseReference ref = database.getReference("User");
好像你忘了初始化
FirebaseDatabase database = FirebaseDatabase.getInstance();
初始化数据库,然后初始化 ref (数据库参考(。
为了写给你的根"用户",
User user =new User(
Name = etName.text.toString(),
Phone = etPhone.text.toString(),
password = etPassword.text.toString()
)
database.child(userId).setValue(user)