0

I am trying to sign in a user with existing id but the app crashes when the sign in button is pressed.I don't want to use email as sign in I want to use the username as sign in.

JAVA CODE

public class login extends AppCompatActivity {
    Button sign;
    TextInputLayout usr,pass;
    private View view;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);



        sign = findViewById(R.id.signin);
        sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginUser(view);
            }
        });
    }
    public void loginUser(View view)
    {
        if(!validateusrName()|!validateemail())
        {return;}
        else {
            isUser();
        }
    }
    private void isUser(){
        final String userEnteredusrname = usr.getEditText().getText().toString().trim();
        final String userEnteredpass = pass.getEditText().getText().toString().trim();

        DatabaseReference reference= FirebaseDatabase.getInstance().getReference("users");
        Query checkUser = reference.orderByChild("usrname").equalTo(userEnteredusrname);
        checkUser.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if(dataSnapshot.exists()){
                    usr.setError(null);
                    usr.setErrorEnabled(false);
                    String passfrmdb=dataSnapshot.child(userEnteredusrname).child("pass").getValue(String.class);
                    if(passfrmdb.equals(userEnteredpass)){
                        usr.setError(null);
                        usr.setErrorEnabled(false);
                        String dbcllg=dataSnapshot.child(userEnteredusrname).child("cllg").getValue(String.class);
                        String dbemail=dataSnapshot.child(userEnteredusrname).child("email").getValue(String.class);
                        String dbname=dataSnapshot.child(userEnteredusrname).child("name").getValue(String.class);
                        String dbpass=dataSnapshot.child(userEnteredusrname).child("pass").getValue(String.class);
                        String dbphno=dataSnapshot.child(userEnteredusrname).child("phno").getValue(String.class);
                        String dbusrname=dataSnapshot.child(userEnteredusrname).child("usrname").getValue(String.class);

                        Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                        intent.putExtra("name",dbname);
                        intent.putExtra("usrname",dbusrname);
                        intent.putExtra("email",dbemail);
                        intent.putExtra("pass",dbpass);
                        intent.putExtra("phno",dbphno);
                        intent.putExtra("cllg",dbcllg);

                        startActivity(intent);
                    }
                    else{
                        pass.setError("Wrong Password");
                        pass.requestFocus();
                    }
                }

                else {
                    usr.setError("NO SUCH USER FOUND");
                    usr.requestFocus();
                }
            }


            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });


    }

LOG CAT

2020-04-20 00:23:56.279 32531-32531/com.studenthelper.bscithelp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.studenthelper.bscithelp, PID: 32531
java.lang.IllegalStateException: Could not execute method for android:onClick
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)
    at android.view.View.performClick(View.java:6605)
    at android.view.View.performClickInternal(View.java:6582)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25897)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6692)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
    at android.view.View.performClick(View.java:6605) 
    at android.view.View.performClickInternal(View.java:6582) 
    at android.view.View.access$3100(View.java:778) 
    at android.view.View$PerformClick.run(View.java:25897) 
    at android.os.Handler.handleCallback(Handler.java:873) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6692) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.widget.EditText com.google.android.material.textfield.TextInputLayout.getEditText()' on a null object reference
    at com.studenthelper.bscithelp.login.validateusrName(login.java:35)
    at com.studenthelper.bscithelp.login.loginUser(login.java:62)
    at java.lang.reflect.Method.invoke(Native Method) 
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397) 
    at android.view.View.performClick(View.java:6605) 
    at android.view.View.performClickInternal(View.java:6582) 
    at android.view.View.access$3100(View.java:778) 
    at android.view.View$PerformClick.run(View.java:25897) 
    at android.os.Handler.handleCallback(Handler.java:873) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6692) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
Mike M.
  • 35,854
  • 7
  • 92
  • 90
abhishek
  • 35
  • 1
  • 6
  • 2
    please also post the XML file – Nilesh B Apr 19 '20 at 19:28
  • I would guess that `usr` and/or `pass` are null are not initialised. – halfer Apr 19 '20 at 20:19
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – halfer Apr 19 '20 at 20:20
  • Please don't fundamentally change your question like that, especially after answers have been posted. One specific issue per post, please. If you now have a new problem, you'll need to post a new question. – Mike M. Apr 19 '20 at 20:37
  • okay but can u help me now . The issue remain same i cant sign in – abhishek Apr 19 '20 at 20:38
  • Given that any changes would now invalidate the good answers received, you now need to ask a new question (of course after doing an appropriate amount of research and debugging yourself). – halfer Apr 19 '20 at 20:45

3 Answers3

2

In method isUser() you are using your usr and pass TextInputLayouts. But you are not assigning value to those variables.

You fix it, just add in your onCreate() method new lines which set value of those variables (using findViewById() method). Just after setContentView() (which is reponsible for inflating layout), add new lines:

usr = findViewById.(R.id.usr);     // Use here your name from xml layout file!!!
pass = findViewById.(R.id.pass);   // Use here your name from xml layout file!!!

So onCreate() should look like there:

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

    usr = findViewById(R.id.usr);      //  <---- LINE TO ADD
    pass = findViewById(R.id.pass);    //  <---- LINE TO ADD

    sign = findViewById(R.id.signin);
    sign.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            loginUser(view);
        }
    });
}
Boken
  • 3,207
  • 9
  • 25
  • 31
  • @abhishek is my solution working for you? – Boken Apr 20 '20 at 00:45
  • it works but got a problem https://stackoverflow.com/q/61311511/12981063 – abhishek Apr 20 '20 at 08:43
  • In new question, your `passfrmdb` is `null`. So you CAN NOT call method `equals` on it. Probably your line `dataSnapshot.child(userEnteredusrname).child("pass").getValue(String.class);` returns `null`. So You have to add some extra condition like `if (passfrmdb != null)` – Boken Apr 20 '20 at 08:46
0

As can see your crash report, you have declare android:onClick in your XML file, and that method might not defined in your class file.

Whatever method you are calling from there, either call it from XML or from your class file and make sure you have define that method in your class file also.

And also make sure that every widgets (TextInputLayout or button or whatever) you are using for, they all must have initialize before use them.

Nilesh B
  • 742
  • 1
  • 7
  • 12
-1

@abhishek, when you try to get the value of the textInputLayout, your code doesn't know where to go because you never linked it to the XML.

try

usr = findViewById(R.id.usr_id);
pass = findViewById(R.id.pass);

This should work.

Zain
  • 13,030
  • 5
  • 26
  • 49
Mr. Octodood
  • 111
  • 8