gpt4 book ai didi

java - 无法在 child() firebase 错误中为参数 'pathString' 传递 null

转载 作者:行者123 更新时间:2023-12-02 11:17:34 25 4
gpt4 key购买 nike

这是代码,当我想要获取用户 key (getPhone)时我遇到问题,我不能,因为获取空值我是编程新手。

    LayoutInflater inflater = LayoutInflater.from(this);
View layout_pwd = inflater.inflate(R.layout.ganti_password_layout,null);

final MaterialEditText edtPassword = (MaterialEditText)layout_pwd.findViewById(R.id.gantiPassword);
final MaterialEditText edtNewPassword = (MaterialEditText)layout_pwd.findViewById(R.id.gantiPasswordBaru);
final MaterialEditText edtRepeatPassword = (MaterialEditText)layout_pwd.findViewById(R.id.gantiPasswordBaruUlang);

alertDialog.setView(layout_pwd);

//Button
alertDialog.setPositiveButton("GANTI", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Ganti Password Disini
final android.app.AlertDialog waitingDialog = new SpotsDialog(Home.this);
waitingDialog.show();

//cek password lama
if(edtPassword.getText().toString().equals(Common.currentUser.getPassword()))
{
//Cek Password Baru dan Ulang Password
if(edtNewPassword.getText().toString().equals(edtRepeatPassword.getText().toString()))
{
Map<String,Object> passwordUpdate = new HashMap<>();
passwordUpdate.put("password",edtNewPassword.getText().toString());

//Update
DatabaseReference user = FirebaseDatabase.getInstance().getReference("User");
user.child(Common.currentUser.getPhone())
.updateChildren(passwordUpdate)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
waitingDialog.dismiss();
Toast.makeText(Home.this, "Password Telah Diganti", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Home.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
else
{
waitingDialog.dismiss();
Toast.makeText(Home.this, "Pengulangan Password Tidak Sesuai", Toast.LENGTH_SHORT).show();
}
}
else
{
waitingDialog.dismiss();
Toast.makeText(Home.this, "Password Lama Salah", Toast.LENGTH_SHORT).show();
}

}
});

这就是错误

Process: com.example.novaldi.sabilulunganlaykes, PID: 15878 java.lang.NullPointerException: Can't pass null for argument 'pathString' in child() at com.google.firebase.database.DatabaseReference.child(Unknown Source:6) at com.example.novaldi.sabilulunganlaykes.Home$4.onClick(Home.java:259) at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:162) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

这是我的结构数据库 database structure

这是我的用户类别

public User(){

}

public User(String nama, String password, String kodeKeamanan) {
Nama = nama;
Password = password;
IsStaff = "false";
this.KodeKeamanan = kodeKeamanan;
}

public String getKodeKeamanan() {
return KodeKeamanan;
}

public void setKodeKeamanan(String kodeKeamanan) {
KodeKeamanan = kodeKeamanan;
}

public String getPhone() {
return Phone;
}

public void setPhone(String phone) {
Phone = phone;
}

public String getIsStaff() {
return IsStaff;
}

public void setIsStaff(String isStaff) {
IsStaff = isStaff;
}

public String getNama() {
return Nama;
}

public void setNama(String nama) {
Nama = nama;
}

public String getPassword() {
return Password;
}

public void setPassword(String password) {
Password = password;
}

最佳答案

存在此问题的原因是方法 Common.currentUser.getPhone() 返回 null。

要修复此问题,只需在 child() 方法调用之前添加简单的 null 测试即可。

像这样:

String phone = Common.currentUser.getPhone();
if (phone != null)
user.child(phone)
.updateChildren(passwordUpdate)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
waitingDialog.dismiss();
Toast.makeText(Home.this, "Password Telah Diganti", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Home.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
else {
// do something else here
}

或者编辑 getPhone() 方法,使其永远不会返回 null

关于java - 无法在 child() firebase 错误中为参数 'pathString' 传递 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50169366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com