I use firebase database for registering users with custom field. Suddenly I get this unexpected error. I didn't see this error before.
FATAL EXCEPTION: main Process:com.example.muhammadtehmoor.testing, PID: 23770 java.lang.NullPointerException:Attempt to invoke virtual method android.text.Editable android.widget.EditText.getText()' on a null object reference com.example.muhammadtehmoor.testing SignUpActivity$1 ..onClick(SignUpActivity.java:49)
This is my Sign Up activity:
package com.example.muhammadtehmoor.testing;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class SignUpActivity extends AppCompatActivity {
EditText txtname,txtphone,txtemail,txtpassword;
String fullname="",email="",phone="",password = "";
Button submit,cancel;
private FirebaseAuth firebaseAuth;
private DatabaseReference databaseReference;
private FirebaseDatabase firebaseDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
txtname = (EditText) findViewById(R.id.edittext2);
txtphone = (EditText)findViewById(R.id.editText3);
txtemail = (EditText)findViewById(R.id.editText4);
txtpassword = (EditText)findViewById(R.id.editText5);
submit = (Button)findViewById(R.id.btn_submit);
cancel = (Button)findViewById(R.id.btn_cancel);
databaseReference = FirebaseDatabase.getInstance().getReference("Driver");
firebaseAuth = FirebaseAuth.getInstance();
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fullname = txtname.getText().toString();
email = txtemail.getText().toString();
phone = txtphone.getText().toString();
password = txtpassword.getText().toString();
if(TextUtils.isEmpty(fullname))
{
txtname.setError("Enter a valid credential");
txtname.requestFocus();
return;
}
if(TextUtils.isEmpty(email))
{
txtemail.setError("Enter a valid credential");
txtemail.requestFocus();
return;
}
if(TextUtils.isEmpty(phone))
{
txtphone.setError("Enter a valid credential");
txtphone.requestFocus();
return;
}
if(TextUtils.isEmpty(password))
{
txtpassword.setError("Enter a valid credential");
txtpassword.requestFocus();
return;
}
if(password.length()<6)
{
txtpassword.setError("password too short");
txtpassword.requestFocus();
return;
}
firebaseAuth.createUserWithEmailAndPassword(txtemail.getText().toString(), txtpassword.getText().toString())
.addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Driver object = new Driver(
fullname,
phone,
email
);
FirebaseDatabase.getInstance().getReference("Driver")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(object).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
// next.setVisibility(View.GONE);
//back.setVisibility(View.GONE);
Toast.makeText(SignUpActivity.this, "Registeration Complete", Toast.LENGTH_SHORT).show();
startActivity(new Intent(SignUpActivity.this, LoginActivity.class));
} else {
Toast.makeText(SignUpActivity.this, "Account creation failed", Toast.LENGTH_SHORT).show();
}
}
});
//updateUserInfo(pickedImageUri,firebaseAuth.getCurrentUser());
} else {
Toast.makeText(SignUpActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
// ...
}
});////firebase auth
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
This is my Sign Up XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.muhammadtehmoor.testing.SignUpActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="Sign Up Form"
android:textSize="35dp"
android:gravity="center"
android:textColor="@android:color/black"/>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"
android:textSize="20dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="Enter your Name" />
<EditText
android:id="@+id/editText3"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"
android:textSize="20dp"
android:ems="10"
android:inputType="phone"
android:hint="Enter your phone no" />
<EditText
android:id="@+id/editText4"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"
android:textSize="20dp"
android:ems="10"
android:inputType="textEmailAddress"
android:hint="Enter your email" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:passwordToggleEnabled="true"
app:hintEnabled="true"
app:passwordToggleDrawable="@drawable/passwordlock">
<EditText
android:id="@+id/editText5"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"
android:textSize="20dp"
android:ems="10"
android:inputType="textPassword"
android:hint="Enter your password" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/btn_submit"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Submit"
android:background="@drawable/button_design"
android:layout_marginLeft="100dp"
android:drawableRight="@drawable/ic_send_black_24dp"
android:drawableTint="@color/colorPrimaryDark"
android:paddingRight="10dp"
android:layout_marginTop="20dp"
android:textColor="@android:color/black"/>
<Button
android:id="@+id/btn_cancel"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:background="@drawable/button_design"
android:drawableRight="@drawable/ic_cancel_black_24dp"
android:drawableTint="@color/colorPrimaryDark"
android:paddingRight="10dp"
android:text="Cancel"
android:textColor="@android:color/black"
tools:ignore="InvalidId" />
</LinearLayout>
</ScrollView>
</RelativeLayout>