-1

i'm trying to retrieve data from firebase database i have finished the code with everything except for 1 minor error which every time i click to go to profile activity i'm getting this error: "Attempt to invoke virtual method 'java.lang.String com.example.logindesign.Users.getFullName()' on a null object reference"

Here's is my profile activity code. i'm getting the error when i'm calling mName.setText("Name: "+ users.getFullName());

package com.example.logindesign;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

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

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;

import java.util.Objects;

public class Profile extends AppCompatActivity {

    FirebaseAuth firebaseAuth;
    FirebaseUser user;
    FirebaseDatabase firebaseDatabase;
    DatabaseReference databaseReference;
    ImageView imageView;
    TextView mUsername, phoneNo, mEmail,  mName;

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


        firebaseAuth = FirebaseAuth.getInstance();
        user = firebaseAuth.getCurrentUser();
        firebaseDatabase = FirebaseDatabase.getInstance();
        databaseReference = firebaseDatabase.getReference(firebaseAuth.getUid());

        imageView = findViewById(R.id.imageView2);
        mUsername = findViewById(R.id.textView);
        phoneNo = findViewById(R.id.textView1);
        mEmail = findViewById(R.id.textView2);
        mName = findViewById(R.id.textView3);


        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                Users users = dataSnapshot.getValue(Users.class);

                mName.setText("Name: "+ users.getFullName());
                mUsername.setText("Username: "+users.getmUserName());
                mEmail.setText("Email: "+users.getEmail());
                phoneNo.setText("Phone No: "+users.getMobileNumber());


            }

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

            }
        });




    }
}

and this is my Users Activity Code

package com.example.logindesign;

public class Users {

    public String fullName;
    public String mUserName;
    public String email;
    public String mobileNumber;

    public Users(){

    }

    public Users(String fullName, String mUserName, String email, String mobileNumber) {
        this.fullName = fullName;
        this.mUserName = mUserName;
        this.email = email;
        this.mobileNumber = mobileNumber;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getmUserName() {
        return mUserName;
    }

    public void setmUserName(String mUserName) {
        this.mUserName = mUserName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMobileNumber() {
        return mobileNumber;
    }

    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }
}

any idea on what's causing this error?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Wisam Atil
  • 91
  • 1
  • 9

1 Answers1

1

This line

Users users = dataSnapshot.getValue(Users.class);

returns null. So your users object is null and the follow-up-line

mName.setText("Name: "+ users.getFullName());

causes the NullReferenceException

The solution is

Users users = dataSnapshot.getValue(Users.class);
if (users != null) {
    mName.setText("Name: "+ users.getFullName());
    mUsername.setText("Username: "+users.getmUserName());
    mEmail.setText("Email: "+users.getEmail());
    phoneNo.setText("Phone No: "+users.getMobileNumber());
} else {
    // write a log, write something else to the text boxes...
}

Maybe you need to check whether dataSnapshot even contains data about users? What else could have changed?

To me it looks like you blindly accept that callback without checks what has changed. Maybe this even triggers when data is deleted? Maybe there is other data contained in the snapshot, but not Users?

Grisgram
  • 3,105
  • 3
  • 25
  • 42
  • Ok now the error is gone, Thank you for that. But the data from database is not showing now. – Wisam Atil Feb 27 '20 at 08:46
  • you need to find out, why ``.getValue`` returns null. Read the docs, how this snapshot is structured. how you can query it correctly and how to find out, which contents it can hold. currently it does _not_ hold objects that are of ``Users.class`` type – Grisgram Feb 27 '20 at 08:48