0

I'm a beginner in android studio,and for the following bit of code

package com.example.petsstork;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
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.analytics.FirebaseAnalytics;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class registrationactivity extends AppCompatActivity {
   private EditText emailTextView, passwordTextView;
private Button Btn2;

private FirebaseAuth mAuth;

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

    // taking FirebaseAuth instance
    mAuth = FirebaseAuth.getInstance();

    // initialising all views through id defined above
    emailTextView = findViewById(R.id.email);
    passwordTextView = findViewById(R.id.passwd);
    Btn2 = findViewById(R.id.regitrbtn);
    // Set on Click Listener on Registration button
    Btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            registerNewUser();
        }
    });
}
private void registerNewUser()
{

    // show the visibility of progress bar to show loadin

    // Take the value of two edit texts in Strings
    String email, password;
    EditText emailTextView = findViewById(R.id.email);
    EditText passwordTextView = findViewById(R.id.passwd);
    email = emailTextView.getText().toString();
    Button btn2;
    password = passwordTextView.getText().toString();

    // Validations for input email and password
    if (TextUtils.isEmpty(email)) {
        Toast.makeText(getApplicationContext(),
                        "Please enter email!!",
                        Toast.LENGTH_LONG)
                .show();
        return;
    }
    if (TextUtils.isEmpty(password)) {
        Toast.makeText(getApplicationContext(),
                        "Please enter password!!",
                        Toast.LENGTH_LONG)
                .show();
        return;
    }

    // create new user or register new user
    mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {

                @Override
                public void onComplete(@NonNull Task<AuthResult> task)
                {
                    if (task.isSuccessful()) {
                        Toast.makeText(getApplicationContext(),
                                        "Registration successful!",
                                        Toast.LENGTH_LONG)
                                .show();

                        // hide the progress bar

                        // if the user created intent to login activity
                        Intent intent = new Intent(registrationactivity.this, MainActivity.class);
                        startActivity(intent);
                    }
                    else {

                        // Registration failed
                        Toast.makeText(
                                        getApplicationContext(),
                                        "Registration failed!!"
                                                + " Please try again later",
                                        Toast.LENGTH_LONG)
                                .show();

                        // hide the progress bar

                    }
                }
            });
}
    // Take the value of two

} '''

Im getting the following error Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference at com.example.petsstork.registrationactivity.registerNewUser(registrationactivity.java:57) at com.example.petsstork.registrationactivity.access$000(registrationactivity.java:21) at com.example.petsstork.registrationactivity$1.onClick(registrationactivity.java:45) at android.view.View.performClick(View.java:7448) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1194) at android.view.View.performClickInternal(View.java:7425) at android.view.View.access$3600(View.java:810) at android.view.View$PerformClick.run(View.java:28305) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

0 Answers0