0

I want to display User information on my fragment on Android Studio. When I finishing the code it got no error. But when I ran my aplication on my device, when I go to the Account Fragment the application is stopped working. When I check the log cat it says:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.ghifa.mrad.Model.User.getImageurl()'

I dont know what should I fix. Can someone help me? This is for school task.

This is my logcat error

11-20 17:46:19.829 14051-14051/com.example.ghifa.mrad E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.ghifa.mrad, PID: 14051
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.ghifa.mrad.Model.User.getImageurl()' on a null object reference
        at com.example.ghifa.mrad.AkunFrg$2.onDataChange(AkunFrg.java:107)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.4:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.4:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.4:55)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:160)
        at android.app.ActivityThread.main(ActivityThread.java:5541)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)

And this is my Java

ImageView photo;
ImageView back;

TextView topNama;
TextView topEmail;
TextView mSaldo;
TextView mUsername;
TextView mNama;
TextView mEmail;
TextView mHP;
TextView mAlamat;

Button toEdit;


Uri uriProfileimage;

FirebaseUser firebaseUser;
String profileid;

public AkunFrg() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_akun_frg, container, false);

    firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

    SharedPreferences prefs = getContext().getSharedPreferences("PREFS", Context.MODE_PRIVATE);
    profileid = prefs.getString("profileid", "none");

    photo = view.findViewById(R.id.foto);
    topNama = view.findViewById(R.id.ac_top_nama);
    topEmail = view.findViewById(R.id.ac_top_email);
    mSaldo = view.findViewById(R.id.saldo_akun);
    mUsername = view.findViewById(R.id.ac_username);
    mNama = view.findViewById(R.id.ac_nama);
    mEmail = view.findViewById(R.id.ac_email);
    mHP = view.findViewById(R.id.ac_hp);
    mAlamat = view.findViewById(R.id.ac_alamat);
    toEdit = view.findViewById(R.id.to_edit);

    userinfo();

    if(profileid.equals(firebaseUser.getUid()))
    {
        toEdit.setText("Edit Profil");
    }

    toEdit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    return view;
}

private void userinfo()
{
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Users").child(profileid);
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            if(firebaseUser != null)
            {
                User user = dataSnapshot.getValue(User.class);

                Glide.with(getContext()).load(user.getImageurl()).into(photo);
                topNama.setText(user.getNama());
                mUsername.setText(user.getUsername());
                mSaldo.setText(user.getSaldo());
                mNama.setText(user.getNama());
                mHP.setText(user.getNo_hp());
                mAlamat.setText(user.getAlamat());
            }

        }

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

        }
    });
}

}

Ghifari
  • 19
  • 1
  • 8
  • Your `user` object is null in `onDataChange` method you should wrap entire code with `if(user != null)` once assignment. – Jeel Vankhede Nov 20 '18 at 15:30
  • Hit the breakpoint on this line `Glide.with(getContext()).load(user.getImageurl()).into(photo);` and check if user is null – Roman Soviak Nov 20 '18 at 15:32
  • I want to give some pieces of advice: -to my mind vengerian notaion is useless in this case -remember about ctrl+alt+l -remove emty lines and double empty lines if you don't require them -`FirebaseDatabase.getInstance().getReference().child("Users").child(profileid)` use Builder pattern style -remove unuseful comments -make String constants, don't hardcode strings -give meaningful names – Roman Soviak Nov 20 '18 at 15:43

2 Answers2

2

before writing this

Glide.with(getContext()).load(user.getImageurl()).into(photo);

make sure you check the value exist

like this

if(user.getImageurl() != null){
    Glide.with(getContext()).load(user.getImageurl()).into(photo);
}

or put the code in try/catch statement like this

try{
   Glide.with(getContext()).load(user.getImageurl()).into(photo);
}catch(NullPointerException e){
   Log.e(TAG, e.toString())
}

hope it helps

Harkal
  • 1,770
  • 12
  • 28
0

from https://firebase.google.com/docs/reference/android/com/google/firebase/database/DataSnapshot.html

An instance of the class passed in, populated with the data from this snapshot, or null if there is no data at this location.

I suspect dataSnapshot.getValue(User.class); return null.

Milo Bem
  • 1,033
  • 8
  • 20