Tushar ranjan
Tushar ranjan

Reputation: 11

android.text.Editable android.widget.EditText.getText() on a null object reference

I created a app in which i created login and register activity and also coded its java file . when im executing it and clicking on signup after filling all details , App closed suddenly . then i searched the issue in Run panel and its below error . I tried what ever i can do but i couldn't find the main reason. please help me to clear this issue . I'm tried of it. below error is showing in register.java

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.marvi, PID: 28256
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
        at com.example.marvi.register$1.onClick(register.java:67)
        at android.view.View.performClick(View.java:6256)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
        at android.view.View$PerformClick.run(View.java:24701)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

can anyone tell me , that why its happen

login.java

package com.example.marvi;

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.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class login extends AppCompatActivity {
    EditText memail , mpass ;
    FirebaseAuth fAuth;

    Button mlogin;
    TextView mcreate , mforgot;



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

        mlogin = findViewById(R.id.signbtn);
        mcreate = findViewById(R.id.create);
        mforgot = findViewById(R.id.forgot);
        fAuth = FirebaseAuth.getInstance();
        memail = findViewById(R.id.email);
        mpass = findViewById(R.id.passbox);

        mlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = memail.getText().toString().trim();
                String password = mpass.getText().toString().trim();

                if(TextUtils.isEmpty((email))){
                    memail.setError("E-mail is required");
                }

                if(TextUtils.isEmpty((password))){
                    memail.setError("Password is required");
                }

                if(password.length() <6){
                    mpass.setError("Password must be > 6");
                }

                //authenticate
                fAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful()){
                            Toast.makeText(login.this, "Logged in successfully", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(getApplicationContext(),MainActivity.class));

                        }else{
                            Toast.makeText(login.this, "Error !"+ task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });

            }
        });



        mcreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(),register.class));
                finish();
            }
        });



    }
}
   

register.java

package com.example.marvi;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
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.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;

import java.util.HashMap;
import java.util.Map;

public class register extends AppCompatActivity {
    public static final String TAG = "TAG";
    EditText mfullname,memail,mpass,mphone;
    FirebaseAuth fAuth;
    RadioButton mcheckmale,mcheckfemale;
    String userID;
    String gender="";
    FirebaseFirestore fStore;

    Button mlogin;
    TextView mcreate , mforgot;

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



        mfullname = findViewById(R.id.name);
        memail = findViewById(R.id.email);
        mpass   = findViewById(R.id.pass);
        mphone      = findViewById(R.id.number);
        mcheckmale= findViewById(R.id.malebtn);
        mcheckfemale  = findViewById(R.id.femalebtn);
        fStore = FirebaseFirestore.getInstance();
        fAuth = FirebaseAuth.getInstance();

        mlogin = findViewById(R.id.signbtn);
        mcreate = findViewById(R.id.create);
        mforgot = findViewById(R.id.forgot);

        if(fAuth.getCurrentUser() !=null){
            Toast.makeText(register.this, "user created", Toast.LENGTH_SHORT).show();
            finish();
        }

        mlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = memail.getText().toString().trim();
                String password = mpass.getText().toString().trim();



                if(TextUtils.isEmpty(email)){
                    memail.setError("Email is Required.");
                    return;
                }

                if(TextUtils.isEmpty(password)){
                    mpass.setError("Password is Required.");
                    return;
                }

                if(password.length() < 6){
                    mpass.setError("Password Must be >= 6 Characters");
                    return;
                }

                if(mcheckmale.isChecked()){
                    gender="Male";
                }
                if(mcheckfemale.isChecked()){
                    gender="Female";
                }

                fAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful()){
                            Toast.makeText(register.this, "user created", Toast.LENGTH_SHORT).show();
                            userID = fAuth.getCurrentUser().getUid();
                            DocumentReference documentReference = fStore.collection("users").document(userID);
                            Map<String,Object> user = new HashMap<>();
                            user.put("email",email);
                            user.put("pass",password);
                            user.put("gender",gender);


                            documentReference.set(user).addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    Log.d(TAG, "onSuccess: user profile is created for " +userID);

                                }
                            });

                            startActivity(new Intent(getApplicationContext(),MainActivity.class));

                        }else{
                            Toast.makeText(register.this, "Error !"+task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });



        mcreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(),login.class));
                finish();
            }
        });


    }
}

Upvotes: 1

Views: 1407

Answers (2)

Jose arias
Jose arias

Reputation: 1

Wrong reference, on the Register class for editText. The "id = memail" you tried to referenece is the one on the wrong layout. instead of calling the two of them by the same name use a different name. either that or use different id names for different layouts. for example...

EditText android:id="@+id/editTextEmail

Upvotes: 0

Soumik Bhattacharjee
Soumik Bhattacharjee

Reputation: 945

Instantiate your edit texts like this on the onCreate() method:

memail = findViewById(R.id.yourEmailFieldID);
mpass = findViewById(R.id.yourPasswordFieldID);

Upvotes: 1

Related Questions