尝试在空对象引用上调用虚方法'boolean java.lang.String.equals(java.lang.Object)'

时间:2018-02-08 07:15:04

标签: java android firebase firebase-realtime-database firebase-authentication

 This is the errror, tried a few things but still to no avail.

java.lang.NullPointerException:尝试在com.dllibrary.cart上的空对象参考 e上调用虚方法'boolean java.lang.String.equals(java.lang.Object)' .SigninMainActivity $ 1 $ 1.onDataChange(的 SigninMainActivity.java:56)                                                                         在com.google.android.gms.internal.zzegf.zza(未知来源)                                                                         在com.google.android.gms.internal.zzeia.zzbyc(未知来源)                                                                         在com.google.android.gms.internal.zzeig.run(未知来源)                                                                         在android.os.Handler.handleCallback(Handler.java:815)                                                                         在android.os.Handler.dispatchMessage(Handler.java:104)                                                                         在android.os.Looper.loop(Looper.java:194)                                                                         在android.app.ActivityThread.main(ActivityThread.java:5637)                                                                         at java.lang.reflect.Method.invoke(Native Method)                                                                         在java.lang.reflect.Method.invoke(Method.java:372)                                                                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:960)                                                                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

我登录方法的代码

     signin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

           final ProgressDialog mdialog = new       ProgressDialog(SigninMainActivity.this);
            mdialog.setMessage("Please waiting");
            mdialog.show();
            table_user.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    //check if user not exist
                    if (dataSnapshot.child(phone.getText().toString()).exists()) {
                        //Get User Information
                        mdialog.dismiss();

                        User user = dataSnapshot.child(phone.getText().toString()).getValue(User.class);
                       if (user.getPassword().equals(password.getText().toString())) {
                            Intent homeIntent= new Intent(SigninMainActivity.this, home.class);
                            Common.currentUser = user;
                            startActivity(homeIntent);
                            finish();
                        } else {

                            Toast.makeText(SigninMainActivity.this, "Sign in failed", Toast.LENGTH_SHORT).show();


                        }
                    }else{
                        mdialog.dismiss();
                        Toast.makeText(SigninMainActivity.this, "User Does Not exist", Toast.LENGTH_SHORT).show();
                    }
                }

第56行位于“if(user.getPassword.equals)”

2 个答案:

答案 0 :(得分:2)

以下错误:

Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

告诉你这是什么。这意味着在这行代码中:

if (user.getPassword().equals(password.getText().toString()))

存在于56行,user.getPassword()返回null

这种情况正在发生,因为您尝试从数据库获取的User对象的值为null。因此,要解决此问题,请确保获得正确的User对象。另外,在实际使用它之前,请检查此对象是否为null:

if(user != null) {}

答案 1 :(得分:1)

使用我预期的更改检查下面的代码。

    signin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               final ProgressDialog mdialog = new       ProgressDialog(SigninMainActivity.this);
                mdialog.setMessage("Please waiting");
                mdialog.show();
                table_user.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        //check if user not exist
                        if (dataSnapshot.child(phone.getText().toString()).exists()) {
                            //Get User Information
                            mdialog.dismiss();

                            User user = dataSnapshot.child(phone.getText().toString()).getValue(User.class);
//Check the null
                           if (user.getPassword() != null && password.getText() != null && user.getPassword().equals(password.getText().toString())) {
                                Intent homeIntent= new Intent(SigninMainActivity.this, home.class);
                                Common.currentUser = user;
                                startActivity(homeIntent);
                                finish();
                            } else {

                                Toast.makeText(SigninMainActivity.this, "Sign in failed", Toast.LENGTH_SHORT).show();


                            }
                        }else{
                            mdialog.dismiss();
                            Toast.makeText(SigninMainActivity.this, "User Does Not exist", Toast.LENGTH_SHORT).show();
                        }
                    }