0

I have been trying lately but I just can't find where is the null object, help me understanding this error:

2021-01-24 01:10:47.244 24211-24211/com.salmanibrahim.firebasechat E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.salmanibrahim.firebasechat, PID: 24211
    java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.salmanibrahim.firebasechat.model.User.name' on a null object reference
        at com.salmanibrahim.firebasechat.ui.UserProfileFragment.setupArrayListInfo(UserProfileFragment.java:243)
        at com.salmanibrahim.firebasechat.ui.UserProfileFragment$1.onDataChange(UserProfileFragment.java:93)
        at com.google.firebase.database.Query$1.onDataChange(Query.java:189)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
        at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:101)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7529)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

(UserProfileFragment.java:243)

Configuration userNameConfig = new Configuration(USERNAME_LABEL, myAccount.name, R.mipmap.ic_account_box);

(UserProfileFragment.java:93)

setupArrayListInfo(myAccount);

The app runs correctly, login is succesfull but as soon as code reaches this point:

if (listFriendID == null) {
            listFriendID = new ArrayList<>();
            dialogFindAllFriend.setCancelable(false)
                    .setIcon(R.drawable.ic_add_friend)
                    .setTitle("Fetching Friends Data...")
                    .setTopColorRes(R.color.colorPrimary)
                    .show();
            getListFriendUId();
        }

Dialog Appears correctly but when it ends loading the app crashes then and there. Implementation for getListFriendUId();:

private void getListFriendUId() {
        FirebaseDatabase.getInstance().getReference().child("friend/" + StaticConfig.UID).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.getValue() != null) {
                    HashMap mapRecord = (HashMap) dataSnapshot.getValue();
                    Iterator listKey = mapRecord.keySet().iterator();
                    while (listKey.hasNext()) {
                        String key = listKey.next().toString();
                        listFriendID.add(mapRecord.get(key).toString());
                    }
                    getAllFriendInfo(0);
                } else {
                    dialogFindAllFriend.dismiss();
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }

I just can't figure out, where to look for error.

Salman Malik
  • 923
  • 6
  • 24
  • Does this answer your question? [Avoiding NullPointerException in Java](https://stackoverflow.com/questions/271526/avoiding-nullpointerexception-in-java) – Walid Jan 23 '21 at 20:41
  • Try to place your application in debug mode prior to the error and see which field is null. After that, go and see if it was properly initialized and if not, why not. – Petre Popescu Jan 23 '21 at 20:42
  • "I just can't figure out, where to look for error." look at UserProfileFragment.java line 243 in the method setupArrayListInfo() – Walid Jan 23 '21 at 20:44
  • Thank you @PetrePopescu, The error was my mistake. I manually created the user onfirebase instead of using signup feature throgh the app. It actually was failing while getting username which, due to manually creating user, was missing. – Salman Malik Jan 23 '21 at 20:54

1 Answers1

-1

You may not have a properly initialized instance of the ArrayList object. In your line:

listFriendID = new ArrayList<>();

You are missing a parameter for the value that the ArrayList will receive. For example:

listFriendID = new ArrayList<Integer>(); // I assume that listFriendID is an Integer
Petr Fořt Fru-Fru
  • 858
  • 2
  • 8
  • 23