Muddz Muddz - 1 month ago 7
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

newInstance(..);
is called
and in the Fragments
onCreateView();
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();
createTrainingFragment.setArguments(bundle);


return createTrainingFragment;
}


Here I use getArguments(); and feed the arguments into Views:
Would the default 0 variable not automatically be inserted into my
sbduration.setProgress();
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();

@Nullable
@Override
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(R.id.seekbar_time);
sbDistance = (SeekBar) v.findViewById(R.id.seekbar_distance);


txtduration = (TextView) v.findViewById(R.id.time_value);
txtDistance = (TextView) v.findViewById(R.id.distance_value);


sbduration.setMax(100);
sbDistance.setMax(50);



initArgumentsData();
}


From RecyclerView I start a new fragment instance like this:

itemView.setOnClickListener(new View.OnClickListener() {
@Override
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);

fragmentManager.beginTransaction().replace(R.id.navdrawer_maincontainer,createTrainingFragment).addToBackStack(null).commit();

}
});


From the add button the Fragment is started like this:

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

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

}
});

Answer
getFragmentManager().beginTransaction().replace(R.id.navdrawer_maincontainer,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(R.id.navdrawer_maincontainer, CreateTrainingFragment.newInstance()).addToBackStack(null).commit();