I am trying to develop a sign up page for an app. but i have encountered this error.
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference .I know what it means, but i have looked at the code and also found several posts about Null Pointer Exception, but it seems fine .Can someone review my code what am i missing please here is the code. in addition when i click on the error it comes that line of code if(dataSnapshot.child(edtPhone.getText().toString()).exists())
so i guess problem is somewhere there
public class SignUp extends AppCompatActivity {
EditText edtPhone,edtName,edtPassword;
Button btnSignUp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
edtName = (EditText) findViewById(R.id.edtName);
edtPassword = (EditText)findViewById(R.id.edtPassword);
edtPhone = (EditText)findViewById(R.id.edtPhone);
btnSignUp = (Button)findViewById(R.id.btnSignUp);
//Init Firebase
final FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference table_user = database.getReference("User");
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ProgressDialog mDialog = new ProgressDialog(SignUp.this);
mDialog.setMessage("Please waiting ...");
mDialog.show();
table_user.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//check if already user Phone
if(dataSnapshot.child(edtPhone.getText().toString()).exists())
{
mDialog.dismiss();
Toast.makeText(SignUp.this, "Phone Number already registered", Toast.LENGTH_SHORT).show();
}
else
{
mDialog.dismiss();
User user = new User((edtName.getText().toString()),edtPassword.getText().toString());
table_user.child(edtPhone.getText().toString()).setValue(user);
Toast.makeText(SignUp.this, "Sign Up successful", Toast.LENGTH_SHORT).show();
finish();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
}
and that's the Main Activity sign up
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"
android:background="@drawable/coffee_pot_backround"
tools:context=".SignUp">
<EditText
android:id="@+id/edtPhone"
android:layout_width="276dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="132dp"
android:layout_marginEnd="119dp"
android:layout_marginRight="119dp"
android:ems="10"
android:hint="Phone Number"
android:inputType="phone"
android:textColor="@color/common_google_signin_btn_text_dark_focused"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/edtName"
android:layout_width="272dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="59dp"
android:layout_marginEnd="123dp"
android:layout_marginRight="123dp"
android:ems="10"
android:hint="User Name"
android:inputType="textPersonName"
android:textColor="@color/common_google_signin_btn_text_dark_focused"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edtPhone" />
<EditText
android:id="@+id/edtPassword"
android:layout_width="272dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="61dp"
android:layout_marginEnd="123dp"
android:layout_marginRight="123dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:textColor="@color/common_google_signin_btn_text_dark_default"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edtName" />
<Button
android:id="@+id/btnSignUp"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="355dp"
android:layout_height="wrap_content"
android:layout_marginStart="26dp"
android:layout_marginLeft="26dp"
android:layout_marginTop="248dp"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
android:text="Sign Up"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edtPassword" />
any help will be appreciated thanks