2

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)
Tyler V
  • 9,694
  • 3
  • 26
  • 52
  • Do you see your "431" log output before the failure, and have you confirmed that `sbsListener` is non-null after being set in `onAttach`? – Tyler V Apr 03 '22 at 17:52
  • @Tyler V yes, I see the log output with tag 431 and I tried making sbsListener as non-null also, but still showed me the same error – Altaj Virani Apr 03 '22 at 18:26
  • Yes you are correct, there is an issue with getParentFragment(), it is also returning me null. :-/ I want to assign my Fragment to it but I don't know how to do so – Altaj Virani Apr 03 '22 at 18:36
  • Thank you very much @Tyler V for the help, earlier in my HomeFragment where I wanted to show the Bottom Sheet when my button was clicked, I had used requireActivity().getSupportFragmentManager, so instead of that I used this.getChildFragmentManager as per your suggestion and it solved my problem. – Altaj Virani Apr 04 '22 at 13:49
  • Glad to hear it - I added an answer to summarize. If it answers your question feel free to check the green check mark. – Tyler V Apr 04 '22 at 14:25

1 Answers1

0

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.

Tyler V
  • 9,694
  • 3
  • 26
  • 52