Home > Enterprise >  BottomSheetListener throws null pointer exception even after initializing it below in onAttach
BottomSheetListener throws null pointer exception even after initializing it below in onAttach

Time:04-05

I'm creating a modal bottom sheet which will get the filtering criteria from the user. I want to use it with Fragment (HomeFragment), and get the button selected from the BottomSheet inside my HomeFragment by passing the string values inside the onButtonClicked method in interface of my BottomSheetFragment and getting those values in my HomeFragment. Even after initializing it in onAttach, it shows null when I display its state in Log.i.

This is my modal bottom sheet class:

public class StartupSearchBottomSheet extends BottomSheetDialogFragment {
    private StartupSearchBottomSheetListener sbsListener;
    Button show;
    String checkedCatButton, checkedSortButton;

    public View onCreateView(LayoutInflater inflater,  @Nullable ViewGroup container, @Nullable  Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.startupsearch_bottomsheet, container, false);

        show = v.findViewById(R.id.show);

        checkedCatButton = "Technology";
        checkedSortButton = "companyName";

        show.setOnClickListener(v -> {
            Log.i(TAG, "432: "   sbsListener); //shows me null here
            sbsListener.onButtonClicked(checkedCatButton, checkedSortButton);
            dismiss();
        });
        return v;
    }
    
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try{
            Log.i(TAG,  "431: onAttach invoked");
            sbsListener = (StartupSearchBottomSheetListener) getParentFragment();//initialized my listener here
        }catch(ClassCastException e){
            throw new ClassCastException(context   " must implement StartupSearchBottomSheetListener.");
        }
    }
}

This is the error that I'm getting:

2022-04-03 21:38:25.330 4465-4465/com.phtlearning.nivesh E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.phtlearning.nivesh, PID: 4465
    java.lang.NullPointerException: Attempt to invoke interface method 'void com.phtlearning.nivesh.StartupSearchBottomSheet$StartupSearchBottomSheetListener.onButtonClicked(java.lang.String, java.lang.String)' on a null object reference
        at com.phtlearning.nivesh.StartupSearchBottomSheet$1.onClick(StartupSearchBottomSheet.java:183)
        at android.view.View.performClick(View.java:7575)
        at android.view.View.performClickInternal(View.java:7548)
        at android.view.View.access$3600(View.java:837)
        at android.view.View$PerformClick.run(View.java:28933)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:236)
        at android.app.ActivityThread.main(ActivityThread.java:8057)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)

CodePudding user response:

It sounds like your fragment is not properly nested so it has no parent fragment. Make sure to use getChildFragmentManager when adding it instead of getSupportFragmentManager, otherwise getParentFragment() will return null.

It is also a good idea to add null checks like this

sbsListener = (StartupSearchBottomSheetListener) getParentFragment();
if( sbsListener == null ) {
    throw new RuntimeException("Could not get listener");
}

so if a variable you expect to be non-null is null you know that immediately.

  • Related