1

I am trying to implement modal bottom sheet to let user select camera / gallery / remove options in order to upload / remove profile picture.

Bottom sheet is displaying without any issues.But when user clicks on any option - Below mentioned error is being thrown.

Attempt to invoke interface method 'ImageChooserDialog$BottomSheetListener.onOptionsClicked(java.lang.String)' on a null object reference

ImageChooserDialog.java class (Modal bottom sheet):

public class ImageChooserDialog extends BottomSheetDialogFragment {

CircularImageView camera,gallery,remove;

public static final String TAG = "ImageChooserDialog";
private BottomSheetListener bottomSheetListener;


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v =  inflater.inflate(R.layout.fragment_image_choose_dialog, container, false);
    camera = v.findViewById(R.id.imagechooser_camera);
    gallery = v.findViewById(R.id.imagechooser_gallery);
    remove = v.findViewById(R.id.imagechooser_remove);

    camera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (bottomSheetListener != null)
            {
                bottomSheetListener.onOptionsClicked("Camera");
                dismiss();
            }else {
                Toast.makeText(getActivity(), "Initialize", Toast.LENGTH_SHORT).show();
            }

        }
    });

    gallery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (bottomSheetListener != null)
            {
                bottomSheetListener.onOptionsClicked("Gallery");
                dismiss();
            }else {
                Toast.makeText(getActivity(), "Initialize", Toast.LENGTH_SHORT).show();
            }

        }
    });

    remove.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (bottomSheetListener != null)
            {
                bottomSheetListener.onOptionsClicked("Remove");
                dismiss();
            }else {
                Toast.makeText(getActivity(), "Initialize", Toast.LENGTH_SHORT).show();
            }
        }
    });
    return v;
}

@Override
public int getTheme() {
    return R.style.BottomSheetDialogTheme;
}


@Override
public void onAttach(Context context) {
    super.onAttach(context);
    try {
        bottomSheetListener = (BottomSheetListener) getParentFragment();
    }catch (ClassCastException e)
    {
        throw new ClassCastException(context.toString()+"must implement BottomSheetListener");
    }
}

public interface BottomSheetListener{
    void onOptionsClicked(String option);
}

}

EditProfile.java class (Fragment)

public class EditProfile extends Fragment implements ImageChooserDialog.BottomSheetListener {

    //to open modal bottom sheet
private void openOptionsBox()
     {
         ImageChooserDialog fragment= new ImageChooserDialog();
         fragment.show(getFragmentManager(),ImageChooserDialog.TAG);
     }

@Override
public void onOptionsClicked(String option) {
    Toast.makeText(getContext(), ""+option, Toast.LENGTH_SHORT).show();
}

 }

Thanks in advance.

Eswar
  • 33
  • 2
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – a_local_nobody Oct 10 '19 at 07:02
  • can you show the `fragment_image_choose_dialog.xml` file? – TaQuangTu Oct 11 '19 at 07:09

1 Answers1

0

you should init listeners after onCreateView get called (view assigned to fragment).you intialize listeners before layout assigned to bottom sheet fragment.

try this

`
public class ImageChooserDialog extends BottomSheetDialogFragment {

CircularImageView camera,gallery,remove;

public static final String TAG = "ImageChooserDialog";
private BottomSheetListener bottomSheetListener;


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup 
 container, @Nullable Bundle savedInstanceState) {
    View v =  inflater.inflate(R.layout.fragment_image_choose_dialog, container, 
false);   
    return v;
}

@Override
    public void onViewCreated ( @NonNull View view, @Nullable Bundle 
savedInstanceState ) {
        super.onViewCreated( view, savedInstanceState );
      initView()
    }


private fun initView()
{
camera = getView().findViewById(R.id.imagechooser_camera);

gallery = getView().findViewById(R.id.imagechooser_gallery);

remove = getView().findViewById(R.id.imagechooser_remove);

camera.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (bottomSheetListener != null)
        {
           bottomSheetListener.onOptionsClicked("Camera");
            dismiss();
        }else {
            Toast.makeText(getActivity(), "Initialize", Toast.LENGTH_SHORT).show();
        }

    }
});

gallery.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (bottomSheetListener != null)
        {
            bottomSheetListener.onOptionsClicked("Gallery");
            dismiss();
        }else {
            Toast.makeText(getActivity(), "Initialize", Toast.LENGTH_SHORT).show();
        }
    }
});

remove.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (bottomSheetListener != null)
        {
           bottomSheetListener.onOptionsClicked("Remove");
            dismiss();
        }else {
                Toast.makeText(getActivity(), "Initialize", 
Toast.LENGTH_SHORT).show();
            }
        }
    });
}

@Override
public int getTheme() {
    return R.style.BottomSheetDialogTheme;
}


@Override
public void onAttach(Context context) {    
    super.onAttach(context);
    try {
        bottomSheetListener = (BottomSheetListener) getParentFragment();
    }catch (ClassCastException e)
    {
        throw new ClassCastException(context.toString()+"must implement 
        BottomSheetListener");
    }
}

public interface BottomSheetListener{
    void onOptionsClicked(String option);
}    `