i_me_mine
i_me_mine

Reputation: 1433

getting null pointer when passing a bundle

So I have this code in one fragment (Info_Frag)

storeNum = store.getText().toString();
phoneNum = phone.getText().toString();
add = address.getText().toString();
cit = city.getText().toString();
zipCode = zip.getText().toString();
state_picked = myStates.getSelectedItem().toString();

Bundle bundle = new Bundle();
Fragment f = new Fragment();
bundle.putString("store", storeNum);
bundle.putString("phone", phoneNum);
bundle.putString("address", add);
bundle.putString("city", cit);
bundle.putString("zip", zipCode);
bundle.putString("state", state_picked);
f.setArguments(bundle);

I am replacing the fragment like this

Fragment fragment = new StoreInfo_Fragment();
                getFragmentManager()
                        .beginTransaction()
                        .setCustomAnimations(android.R.animator.fade_in,
                                android.R.animator.fade_out)
                        .replace(R.id.storeInfo_fragment_container,
                                fragment).commit();

Then in the other fragment (StoreInfo_Fragment) I call it like this

public class StoreInfo_Fragment extends Fragment {
    View view;  

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {        

        view = inflater.inflate(R.layout.fragment_store_info, container,
                false); 

        String mStore = getArguments().getString("store");
        String mPhone = getArguments().getString("phone");
        String mAddress = getArguments().getString("address");
        String mCity = getArguments().getString("city");
        String mZip = getArguments().getString("zip");
        String mState = getArguments().getString("state");  

        TextView store = (TextView) view.findViewById(R.id.store);
        TextView phone = (TextView) view.findViewById(R.id.phone);
        TextView address = (TextView) view.findViewById(R.id.address);
        TextView city = (TextView) view.findViewById(R.id.city);
        TextView zip = (TextView) view.findViewById(R.id.zip);
        TextView state = (TextView) view.findViewById(R.id.state);

        store.setText(mStore);
        phone.setText(mPhone);
        address.setText(mAddress);
        city.setText(mCity);
        zip.setText(mZip);
        state.setText(mState);      

        return view;
    }
}

Which when launched gives me an error, crashing my app. It tells me I have a null pointer exception from this line

String mStore = getArguments().getString("store");

Obviously that means I am not passing my bundle correctly, and all calls to getArguments() will give NPE's. Where did I go wrong?

Upvotes: 3

Views: 1827

Answers (1)

Ahmad
Ahmad

Reputation: 72533

Change your code to this:

Bundle bundle = new Bundle();
Fragment fragment = new StoreInfo_Fragment();
bundle.putString("store", storeNum);
bundle.putString("phone", phoneNum);
bundle.putString("address", add);
bundle.putString("city", cit);
bundle.putString("zip", zipCode);
bundle.putString("state", state_picked);
fragment.setArguments(bundle);

// You should use getSupportFragmentManager() here, if you want your app to be backwards compatible
getFragmentManager() 
        .beginTransaction()
        .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
        .replace(R.id.storeInfo_fragment_container, fragment)
        .commit();

You are not setting the arguments to the fragment you are calling the transaction on.

Upvotes: 1

Related Questions