如何解决“ java.lang.NullPointerException:无法为child()中的参数'pathString'传递null”?

时间:2018-12-24 03:34:21

标签: android firebase firebase-realtime-database nullpointerexception

我不知道我的代码在哪里出错

我尝试了一些解决方案,但在我的情况下都无法使用

代码:

package com.example.kiit.questionme2;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.kiit.questionme2.Model.User;
import com.google.firebase.FirebaseApp;
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 com.rengwuxian.materialedittext.MaterialEditText;

public class Activity3 extends AppCompatActivity {

MaterialEditText edtnewpassword, edtnewemail, edtuser;//for signingup

FirebaseDatabase database;
DatabaseReference users;

Button signup;

public void goback(View view) {


    Intent intenta = new Intent(getApplicationContext(), MainActivity.class);

    startActivity(intenta);

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_3);

    edtnewemail = (MaterialEditText) findViewById(R.id.newemail);
    edtnewpassword = (MaterialEditText) findViewById(R.id.newpassword);
    edtuser = (MaterialEditText) findViewById(R.id.newuser);

    database = FirebaseDatabase.getInstance();
    users = database.getReference("Users");

    FirebaseApp.initializeApp(this);
    signup = (Button) findViewById(R.id.signup);
    signup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            signUp(edtnewemail.getText().toString(), edtnewpassword.getText().toString(), edtuser.getText().toString());
        }
    });


}

private void signUp(String emu, String pwd, final String use) {

    final User user = new User(edtuser.getText().toString());
    edtnewpassword.getText().toString();
    edtnewemail.getText().toString();

    users.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.child(use).exists()) {
                if (!use.isEmpty()) {
                    if (dataSnapshot.child(use).exists())

                    {
                        Toast.makeText(Activity3.this, "User already exists.", Toast.LENGTH_SHORT).show();
                    } else

                    {
                        users.child(user.getUsername())
                                .setValue(user);
                        Toast.makeText(Activity3.this, "User registration successful.", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

错误:

    E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kiit.questionme2, PID: 6946
java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
    at com.google.firebase.database.DatabaseReference.child(Unknown Source)
    at com.google.firebase.database.DataSnapshot.child(Unknown Source)
    at com.example.kiit.questionme2.Activity3$2.onDataChange(Activity3.java:76)
    at com.google.firebase.database.Query$1.onDataChange(Unknown Source)
    at com.google.android.gms.internal.zzbpx.zza(Unknown Source)
    at com.google.android.gms.internal.zzbqx.zzZS(Unknown Source)
    at com.google.android.gms.internal.zzbra$1.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Application terminated.

我需要输入用户名密码和电子邮件作为输入,并检查是否存在,如果显示,则显示相应的消息,然后使用Firebase中保存的数据成功注册。

2 个答案:

答案 0 :(得分:0)

代替if (dataSnapshot.child(use).exists())

尝试if(use!=null && ! use.isEmpty() && dataSnapshot.child(use).exists()))

答案 1 :(得分:0)

此错误/崩溃是由于以下事实造成的:use在您放置它的代码中,特别是在以下代码行中为空:

dataSnapshot.child(use).exists()

因此,您可以做的是在调用此use方法之前检查signUp是否不为null。这将确保您不会在代码的字段中得到null。

因此上述方法的代码如下所示:

if(use!=null){
  // call this method here
}
else {
 // do something else
}

这将确保您的代码在执行该方法期间而不是在执行该方法之前不会捕获nullPointer。因此,您的应用程序中的流程更加顺畅。