Abdellah Ramadan Abdellah Ramadan - 1 month ago 13
Android Question

Getting null object reference error on an EditText object Android

Please I have connected my layout editText with my Java class EditText but still gets an error while trying to get the text from the text field.

I this error

java.lang.NullPointerException: Attempt to invoke virtual method
'android.text.Editable android.widget.EditText.getText()' on a null object
reference


and here is my code.
at com.ramadanapps.android.fireapp.Register.createUser(Register.java:56)

package com.ramadanapps.android.fireapp;

public class Register extends AppCompatActivity {

private FirebaseAuth mAuth;
private EditText mEmail;
private EditText mPassword;
private Button mRegister;

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

mEmail = (EditText) findViewById(R.id.signUpEmail);
mPassword = (EditText) findViewById(R.id.singUpPassword);
mRegister = (Button) findViewById(R.id.signUpBtn);

mAuth = FirebaseAuth.getInstance();

mRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
createUser();
}
});
}
@Override
public void onStart(){
super.onStart();

FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser != null){
Toast.makeText(Register.this,"Already signed in", Toast.LENGTH_LONG).show();
}
}

private void createUser() {
String email = mEmail.getText().toString();
String password = mPassword.getText().toString();

mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
Toast.makeText(Register.this,"Successful", Toast.LENGTH_LONG).show();
startActivity(new Intent(Register.this, MainActivity.class ));
} else{
Toast.makeText(Register.this,"Failure", Toast.LENGTH_LONG).show();
}
}
});
}
[![enter image description here][1]][1]}


I don't know where is the problem.

Here is my XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.ramadanapps.android.fireapp.Register">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/signUpEmail"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="32dp"
android:hint="Email" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/singUpPassword"
android:layout_below="@+id/signUpEmail"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:hint="Password" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign Up"
android:id="@+id/signUpBtn"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign Up"
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="25dp" />
</RelativeLayout>


The xml has the id signUpEmail and I don't still know where the problem is.

Answer Source

I installed your code on my side with only 2 differences: 1. I use my own firebase DB 2. My MainActivity is different - actually I created empty one just for testing.

The app works fine - I successfully signed-up several times with diffeent (fake) mails and passwords. Everytime I was passed to main activity, and when pressed "back" - successfully moved back to Register activity. (snapshot attached).

No failures so far. When I failed - it is when tried to sign-up on empty fields of mail or password - it's expected, you need validate the form.

Other possible reason of failure that comes in my mind - is in your main activity. May be there in onStart or onCreate methods you refer to non-defined TextViews? Would you like to share it with corresponding XML?

Thanks and good luck! ))

enter image description here