Q:

pass some values from an activity to a fragment and then NullPointerException

I would like to pass some values from an activity to a fragment. Have seen a lot of answers but none resolved my issue
Activity
Bundle bundle1 = new Bundle();
    bundle1.putString("baros",baros);
    bundle1.putString("upsos",upsos);
    bundle1.putString("sex",sex);
    CalculatorFragment calculatorFragment = new CalculatorFragment();
    calculatorFragment.setArguments(bundle1);

Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view =  inflater.inflate(R.layout.fragment_calculator, container, false);

    apotelesma = view.findViewById(R.id.apotelesma);
    Bundle bundle = this.getArguments();
    String name = (String) bundle.get("sex");
    apotelesma.setText(name);
    return view;

}

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object reference
problem is here: String name = (String) bundle.get("sex");
thanks!

A:

you can get the variable from activity in fragment by`
 MainActivity ob1 = (MainActivity) getActivity();
 string var1=ob1.sex;

