Reputation: 357
I am trying to make an account on my own app. but when I press the signup
button app gets closed
and no data I found in the Firebase console.
i can't understand what is wrong with code.
MainActivity.java
package com.example.whatsapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
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 MainActivity extends AppCompatActivity {
private FirebaseAuth auth;
FirebaseDatabase database;
TextView forgotpassword,phonesignup;
Button Signup,Googlesignup,fbsignup;
EditText Name,Password,email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
forgotpassword = findViewById(R.id.forgotpassword);
phonesignup = findViewById(R.id.phonesignup);
Signup = findViewById(R.id.Signup);
auth = FirebaseAuth.getInstance();
database =FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");
forgotpassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent forgotintent = new Intent(v.getContext(), ForgotpasswordActivity.class);
startActivity(forgotintent);
}
});
phonesignup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent =new Intent(v.getContext(),phonesignup.class);
startActivity(intent);
}
});
Signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
auth.createUserWithEmailAndPassword(email.getText().toString(),Password.getText().toString())
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
Toast.makeText(MainActivity.this, "account created", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(MainActivity.this, "failed", Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}
gradle dependencies
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation platform('com.google.firebase:firebase-bom:27.1.0')
implementation 'com.google.firebase:firebase-auth'
implementation 'com.google.firebase:firebase-database:19.7.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.airbnb.android:lottie:3.7.0'
logcat error
04-21 17:25:13.270 29804-29804/com.example.whatsapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.whatsapp, PID: 29804
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.whatsapp.MainActivity$3.onClick(MainActivity.java:56)
at android.view.View.performClick(View.java:4788)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View$PerformClick.run(View.java:19923)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5401)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:919)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:714)
Upvotes: 0
Views: 36
Reputation: 695
You have't attach your EditText with your Xml
EditText Name,Password,email;// this need to be intialize.
Logcat says it all
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
name = findViewById(R.id.<your_id_name_in_xml>);
do the above thing for password and email
Upvotes: 2