Shivam Kapoor
Shivam Kapoor

Reputation: 127

Getting null pointer exception during unit testing in roboelectric

I have button click listener on whose click fragment get replaced,and passes argument on next fragment. Code:-

 Fragment fragment = new CustomList();
            Bundle args = new Bundle();
            args.putString("fragment", "Custom");
            args.putSerializable("productBean", productBean);
            fragment.setArguments(args);
            fragmentManager = getActivity().getSupportFragmentManager();
            fragmentTransaction = fragmentManager
                    .beginTransaction();
            fragmentTransaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_out, R.anim.slide_enter, R.anim.slide_exit);
            fragmentTransaction.replace(R.id.container_body, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

Written test case for button click:-

Button custom_btn = (Button) fragment.getView().findViewById(R.id.custom_btn);
             custom_btn.performClick();

But on execution of test cases,It shows null pointer exception when getArguments on CustomList() fragment.

productsBean = (ProductsBean) getArguments().getSerializable("productsBean");

Upvotes: 1

Views: 420

Answers (1)

user3613906
user3613906

Reputation:

Try change this code:

productsBean = (ProductsBean) getArguments().getSerializable("productsBean");

to this

productsBean = (ProductsBean) getArguments().getSerializable("productBean");

As you can see, you are adding to the bundle the tag "productBean", but when you try to get it you are using the tag "productsBean", where you added the "s" charachter.

I would start with that change.

Upvotes: 1

Related Questions