0

Whenever I try to Login to start the HomeActivity, I get this error even though I double-checked the Getters and Setters in Users. I think this may have to something with this fragment of code mentioned below. It shows the the .getNumber may produce NullPointerException.

if(usersData.getNumber().equals(number)){

Here is a fragment of my LoginActivity

            if(dataSnapshot.child(parentDbName).child(number).exists())
            {
                Users usersData = dataSnapshot.child(parentDbName)
                        .child(number).getValue(Users.class);

                if(usersData.getNumber().equals(number))
                {
                        if(usersData.getPw().equals(pw))
                        {
                        Toast.makeText(Login.this, "Logged In Successfuly!", Toast.LENGTH_SHORT).show();
                        prog1.dismiss();

Every time I try to Login using the existing account stored in Firebase, it says

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

This is what I've generated in Users.java

package com.robihidquicaygabinvalencia.ecommerce.shopmore.Model;

public class Users
{
    private String name, number, pw;

    public Users()
    {

    }

    public Users(String name, String number, String pw) {
        this.name = name;
        this.number = number;
        this.pw = pw;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getPw() {
        return pw;
    }

    public void setPw(String pw) {
        this.pw = pw;
    }
}

And here's what I've got in Logcat.

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at com.robihidquicaygabinvalencia.ecommerce.shopmore.Login$2.onDataChange(Login.java:83)
        at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database@@19.1.0:179)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.1.0:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.1.0:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.1.0:55)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6682)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

1 Answers1

0

You are correct. getNumber() is returning null. This likely means there is no value in the database at the location that you're converting into a Users object, but since you haven't show the exact data at the location you're reading, it's not possible to say for sure. In any event, you should check for that before calling toString() on the result.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • So whatever getters and setters I generated, it should be the same as one in the LoginActivity? I don't know if that's the one you're pointing out....... – Kelvin Robihid Oct 12 '19 at 04:58