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

时间:2018-05-04 07:21:04

标签: java android firebase firebase-realtime-database

这是代码,我有问题,当我想获得用户密钥(getPhone)我不能,导致获取null值我是编程中的新手。

    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();
            }

        }
    });

这是错误

  

过程:com.example.novaldi.sabilulunganlaykes,PID:15878                     java.lang.NullPointerException:无法为child()中的参数'pathString'传递null                         在com.google.firebase.database.DatabaseReference.child(未知来源:6)                         在com.example.novaldi.sabilulunganlaykes.Home $ 4.onClick(Home.java:259)                         在android.support.v7.app.AlertController $ ButtonHandler.handleMessage(AlertController.java:162)                         在android.os.Handler.dispatchMessage(Handler.java:105)                         在android.os.Looper.loop(Looper.java:164)                         在android.app.ActivityThread.main(ActivityThread.java:6541)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                         在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;
}

1 个答案:

答案 0 :(得分:1)

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

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

像这样:

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方法