首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >防火墙防火墙抛出空对象引用

防火墙防火墙抛出空对象引用
EN

Stack Overflow用户
提问于 2018-12-21 16:36:55
回答 1查看 6.8K关注 0票数 1

在帐户创建过程中,Logcat不断抛出空对象引用,同时尝试通过防火墙设置文档创建,代码如下,我的大学课程是java的新版本,因此不确定如何识别问题。

如果帐户在auth中成功创建,则尝试将编辑文本字段中的信息存储到防火墙集合中,该信息由所创建帐户的用户ID标识。

代码语言:javascript
复制
public class CreateAccount extends AppCompatActivity implements 
View.OnClickListener {

private static final String TAG = "EmailPassword";
private EditText AccountEmail;
private EditText AccountPass;
private EditText AccountFirstname;
private EditText AccountSurname;
private EditText AccountTown;
private EditText AccountAge;
private FirebaseAuth mAuth;
public FirebaseFirestore cloudstorage;

@Override
//Code that executes when the activity begins; in this case simply setting the view.
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_account);
    AccountEmail = findViewById(R.id.textEditAccountEmail);
    AccountPass = findViewById(R.id.textEditAccountPass);
    AccountFirstname = findViewById(R.id.textEditAccountFirst);
    AccountSurname = findViewById(R.id.textEditAccountLast);
    AccountTown = findViewById(R.id.textEditAccountTown);
    AccountAge = findViewById(R.id.textEditAccountAge);
    mAuth = FirebaseAuth.getInstance();
    FirebaseFirestore cloudstorage = FirebaseFirestore.getInstance();
    //Auto signout for testing
    FirebaseAuth.getInstance().signOut();


}

public void createAccount(String email, String password) {
    Log.d(TAG, "createAccount:" + email);
    if (!Validate()) {
        return;
    }
    mAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        Log.d(TAG, "createUserWithEmail:success");

                        databasecreate();

                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "createUserWithEmail:failure", task.getException());
                        Toast.makeText(CreateAccount.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                    }

                }
            });
}

public boolean Validate() {
    boolean valid = true;
    String email = AccountEmail.getText().toString();
    if (TextUtils.isEmpty(email)) {
        AccountEmail.setError("Required.");
        valid = false;
    } else {
        AccountEmail.setError(null);
    }
    String password = AccountPass.getText().toString();
    if (TextUtils.isEmpty(password)) {
        AccountPass.setError("Required.");
        valid = false;
    } else {
        AccountPass.setError(null);
    }
    return valid;
}

public void databasecreate() {
    FirebaseUser user = mAuth.getCurrentUser();
    String uid = user.getUid();

    Map<String, Object> userlist = new HashMap<>();
    userlist.put("email", AccountEmail.getText());
    userlist.put("password", AccountPass.getText());
    userlist.put("Forename", AccountFirstname.getText());
    userlist.put("Surname", AccountSurname.getText());
    userlist.put("Town", AccountTown.getText());
    userlist.put("Age", AccountAge.getText());
    userlist.put("UserID", uid);

    cloudstorage.collection("users").document(uid)
            .set(userlist)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Log.d(TAG, "Document successfully written!");
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.w(TAG, "Error creating file", e);
                }
            });
}



@Override
public void onClick(View v) {
    int i = v.getId();
    if (i == R.id.btnCreate) {
        createAccount(AccountEmail.getText().toString(), AccountPass.getText().toString());
    }
}

public void onClickBack(View v) {
    Intent backIntent = new Intent(CreateAccount.this, Login.class);
    CreateAccount.this.startActivity(backIntent);
}
}

日志中的错误:

代码语言:javascript
复制
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.coffeedrive.myquote, PID: 20895
    java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.firestore.CollectionReference com.google.firebase.firestore.FirebaseFirestore.collection(java.lang.String)' on a null object reference
        at com.example.adam.myquote.CreateAccount.databasecreate(CreateAccount.java:137)
        at com.example.adam.myquote.CreateAccount$1.onComplete(CreateAccount.java:92)
        at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-21 16:53:29

在函数databasecreate()中,您要引用类变量cloudstorage。这个变量从来没有初始化过,这就是为什么您要获得一个NPE。在onCreate函数中,您将在onCreate函数的作用域中初始化一个不同的cloudstorage变量。只需更改行:

代码语言:javascript
复制
FirebaseFirestore cloudstorage = FirebaseFirestore.getInstance();

代码语言:javascript
复制
this.cloudstorage = FirebaseFirestore.getInstance();

或者简单的

代码语言:javascript
复制
cloudstorage = FirebaseFirestore.getInstance();

以便初始化类变量。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53888132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档