1

I don't quite sure what when wrong, but I have check the variable name is the same whether it is in a layout xml and the java class, but when I run the apps it give me error which said that

java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.example.g.Model.Ps.ps_name' on a null object reference

I have declare the variable btw.

 firebaseAuth = FirebaseAuth.getInstance();
        currentUser = firebaseAuth.getCurrentUser();
        psID = currentUser.getUid();
        databaseReference = FirebaseDatabase.getInstance().getReference("PersonalShopper").child(firebaseAuth.getUid());

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                PersonalShopper ps = dataSnapshot.getValue(PersonalShopper.class);
                psname.setText(ps.ps_name);
                pspassword.setText(ps.ps_password);
                psemail.setText(ps.ps_email);

            }



 <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Name"
        android:textSize="13dp"/>

    <EditText
        android:id="@+id/psname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <TextView
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Password"
        android:textSize="13dp"/>


    <EditText
        android:id="@+id/pspassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <TextView
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Email"
        android:textSize="13dp"/>

    <TextView
        android:id="@+id/psemail"
        android:hint="email@gmail.com"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />



lylyn
  • 13
  • 1
  • 6
  • You have declared the variable, yes, but that doesn't mean it's not null. I don't know exactly what you're doing wrong, but I believe this is returning null: `dataSnapshot.getValue(PersonalShopper.class);` Time to bust out the debugger. – Christopher Schneider Oct 15 '19 at 15:48
  • The error is null pointer when attempt to read that means it is not able to read text of the `editText` `psname`. – Lalit Fauzdar Oct 15 '19 at 15:48
  • 1
    Just noticed you had a similar issue with a NPE a few days ago. I'd recommend you learn how to handle these, as they're generally easy to resolve. The best thing you can do is learn to debug and set breakpoints. That will allow you to look at the values of variables as the program is running and even execute code while the debugger is paused. – Christopher Schneider Oct 15 '19 at 15:56
  • this is a good intro to debugging your app: https://developer.android.com/studio/debug – Nikos Hidalgo Oct 15 '19 at 15:58

3 Answers3

1

My understanding is that the dataSnapshot is NULL. You should make sure that this variable is not null by lunching your code in debugger and put a breakpoint at this line.

Maybe you could give us a larger scope of your code so we can see where dataSnapshotcomes from.

This link may help you..

EnZo
  • 21
  • 3
1

ps is null:

PersonalShopper ps = dataSnapshot.getValue(PersonalShopper.class);

This means there was no data at the location of the database that you queried. Check your query and the contents of the database to make sure everything is correct.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
0

ps is null : So check if the child have value or not . You can also check the below method it will stop app from crashing and if child value is not null it will show the data.

databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

              if (dataSnapshot.exists()){



                PersonalShopper ps = dataSnapshot.getValue(PersonalShopper.class);
                psname.setText(ps.ps_name);
                pspassword.setText(ps.ps_password);
                psemail.setText(ps.ps_email);

            }

             }
Muntasir Aonik
  • 1,800
  • 1
  • 9
  • 22