Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Sahit penmatcha
Sahit penmatcha
4,555 Points

Passing Data from Activity to Fragment Null Object Reference

I am creating an application where the user clicks a "plus" button on the fragment which takes them to a new activity where they enter data. After clicking "save" on this data, the activity should close and the fragment should display this data. I am getting this error when trying to pass the information from the activity to the fragment:

                                                                                   java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String, java.lang.String)' on a null object reference
                                                                                 at com.example.sahitpenmatcha.scagenda.Fragments.ClassesFragment.onCreateView(ClassesFragment.java:44)
                                                                                 at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
                                                                                 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                                                 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
                                                                                 at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
                                                                                 at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                                                 at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
                                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:158)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

This is the Activity the Data is coming from...

switch (item.getItemId()) { // Respond to the action bar's Up/Home button case android.R.id.home: finish(); return true; case R.id.save_button: //add saving code here to save options classNames[0] = mEnterClassName.getText().toString();

        Bundle bundle = new Bundle();
        bundle.putString("STRING_KEY", classNames[0]);
        mClassesFragment.setArguments(bundle);

        finish();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(mClassesFragment, "classFrag");
        fragmentTransaction.commit();



        return true;
}

This is the Fragment the data needs to get to...

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

className = getArguments().getString("KEY_STRING","Class");


((AppCompatActivity)getActivity()).getSupportActionBar().setTitle(R.string.classes_title);
View view = inflater.inflate(R.layout.fragment_classes, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.timeRecyclerView);


adapter = new ClassAdapter(getActivity());

mRecyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
mRecyclerView.setAdapter(adapter);
ItemTouchHelper.Callback callback = new ItemSwipeHelper(getContext(), adapter);
ItemTouchHelper helper = new ItemTouchHelper(callback);
helper.attachToRecyclerView(mRecyclerView);

FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
assert fab != null;
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        adapter.add(0);
    }
});

adapter.setClassTitle(className);

// Inflate the layout for this fragment
return view;

}

Sahit penmatcha
Sahit penmatcha
4,555 Points

EDIT: I changed "KEY_STRING" to "STRING_KEY" so they are equal, but still same error