-2

I have a fragment that is getting data passed by reference from a Utils class. However, when I ran my code I got a null pointer exception

                                                 --------- beginning of crash
03-20 17:17:18.859 3100-3100/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: uk.ac.napier.newsreader, PID: 3100
                                                 java.lang.NullPointerException: Attempt to invoke virtual method 'com.mindorks.placeholderview.PlaceHolderView com.mindorks.placeholderview.ExpandablePlaceHolderView.addView(java.lang.Object)' on a null object reference
                                                     at uk.ac.napier.newsreader.Nav_Drawer.NewsFragment.onCreateView(NewsFragment.java:39)
                                                     at android.support.v4.app.Fragment.performCreateView(Fragment.java:2189)
                                                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
                                                     at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                                                     at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:757)
                                                     at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2355)
                                                     at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
                                                     at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098)
                                                     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008)
                                                     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
                                                     at android.os.Handler.handleCallback(Handler.java:751)
                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                     at android.os.Looper.loop(Looper.java:154)
                                                     at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
03-20 17:17:18.861 1588-2193/system_process W/ActivityManager:   Force finishing activity uk.ac.napier.newsreader/.MainActivity

The code that is causing this is the line

mExpandableView.addView(new HeadingView(mContext, feed.getHeading()));

Here is my whole fragment

/**
 * A simple {@link Fragment} subclass.
 */

public class NewsFragment extends Fragment {

private ExpandablePlaceHolderView mExpandableView;
private Context mContext;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_news, container, false);


    mContext = getActivity().getApplicationContext();
    mExpandableView = (ExpandablePlaceHolderView) getActivity().findViewById(R.id.expandableView);
    for (Feed feed : Utils.loadFeeds(getActivity().getApplicationContext())) {
        mExpandableView.addView(new HeadingView(mContext, feed.getHeading()));
        for (Info info : feed.getInfoList()) {
            mExpandableView.addView(new InfoView(mContext, info));

        }
    }

    return rootView;
}

}

Am I not allowed to use getActivity() in this instance? I tried getContext but it threw even more errors. Any help is appreciated.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mark Barton
  • 847
  • 6
  • 15
  • 2
    Please edit your question and post the entire Java stack trace associated with this crash. `getActivity()` should be fine here. Moreover, you would have crashed on each of the preceding lines, as they also call methods on the `getActivity()` result. – CommonsWare Mar 20 '17 at 17:30
  • 1
    the crash is not on `getActivity()` it's on this method: `.addView(java.lang.Object)' on a null object reference` – lucianohgo Mar 20 '17 at 17:36
  • After seeing the stack trace, what makes you think this was caused by the getActivity call? Or even by that line at all? – Cruncher Mar 20 '17 at 17:37
  • Have you checked if this returns something? `(ExpandablePlaceHolderView) getActivity().findViewById(R.id.expandableView);` debug and see, please – lucianohgo Mar 20 '17 at 17:37

1 Answers1

3

You problem is this line:

mExpandableView = (ExpandablePlaceHolderView) getActivity().findViewById(R.id.expandableView);

mExpandableView would be null at this point. It gives error when you add a sub view to your null view.

Correct line should be

mExpandableView = (ExpandablePlaceHolderView) rootView.findViewById(R.id.expandableView);
Sharjeel
  • 15,588
  • 14
  • 58
  • 89