Muddz Muddz - 2 months ago 10
Android Question

I keep getting null pointer exception on my getArguments();

From a list, the user can click and item and a fragment will inflate showing the data for the clicked item, where the user also can edit that data and click save to save the edited data.

But from the screen that contains the list is also an add button if the user wants to create a new object.

When the user clicks on an item from the list, a

is called
and in the Fragments
I initilize all variables for that clicked item in the different views. But that is not working well because I keep getting:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String, int)' on a null object reference

newInstance is called from the RecyclerView Adapter onClick():

public static CreateTrainingFragment newInstance(ItemModel itemModel) {

bundle = new Bundle();

bundle.putInt(SB_DURATION, itemModel.getDuration());
bundle.putInt(SB_DISTANCE, itemModel.getDistance());

CreateTrainingFragment createTrainingFragment = new CreateTrainingFragment();

return createTrainingFragment;

Here I use getArguments(); and feed the arguments into Views:
Would the default 0 variable not automatically be inserted into my
if the argument dont exist?

private void initArgumentsData() {

sbduration.setProgress(getArguments().getInt(SB_DURATION, 0));
sbDistance.setProgress(getArguments().getInt(SB_DISTANCE, 0));

txtduration.setText(getArguments().getInt(SB_DURATION, 0) + " min");
txtDistance.setText(getArguments().getInt(SB_DISTANCE, 0) + " km");


Here is how my Views is created and where I use InitArgumentData();

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

sbduration = (SeekBar) v.findViewById(;
sbDistance = (SeekBar) v.findViewById(;

txtduration = (TextView) v.findViewById(;
txtDistance = (TextView) v.findViewById(;



From RecyclerView I start a new fragment instance like this:

itemView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

ItemModel itemModel = realm.getDefaultInstance().where(ItemModel.class).equalTo("timestamp",list.get(getAdapterPosition()).getTimestamp()).findFirst();
CreateTrainingFragment createTrainingFragment = CreateTrainingFragment.newInstance(itemModel, true);



From the add button the Fragment is started like this:

addbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

getFragmentManager().beginTransaction().replace(,new CreateTrainingFragment()).addToBackStack(null).commit();


getFragmentManager().beginTransaction().replace(,new CreateTrainingFragment()).addToBackStack(null).commit();

Here, you're using new CreateTrainingFragment(). Hence, you're not getting the bundle since there is no bundle attach to it. You should call the newInstance method first to get the an object of CreateTrainingFragment and then put it on replace.

getFragmentManager().beginTransaction().replace(, CreateTrainingFragment.newInstance()).addToBackStack(null).commit();