Error of java.lang.NullPointerException when the fragment starts
<p>I have tabs with three different fragments. Attendance, Exam and Result. On some phones when the result fragment starts the application crashes saying. </p>

<blockquote>
  <p>java.lang.NullPointerException: Attempt to invoke virtual method
  'java.lang.Object
  android.content.Context.getSystemService(java.lang.String)' on a null
  object reference</p>
</blockquote>

<p>here is the logcat</p>

<pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
        at android.view.LayoutInflater.from(LayoutInflater.java:220)
        at com.bu.innovate.bustudentportal.Result_Class$ExampleAdapter.&lt;init&gt;(Result_Class.java:886)
        at com.bu.innovate.bustudentportal.Result_Class$results.onPostExecute(Result_Class.java:544)
        at com.bu.innovate.bustudentportal.Result_Class$results.onPostExecute(Result_Class.java:245)
        at android.os.AsyncTask.finish(AsyncTask.java:632)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:5942)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
</code></pre>

<p>and the code its pointing to is this</p>

<pre><code>public class Result_Class extends Fragment implements java.io.Serializable {
    private AnimatedExpandableListView listView;
    private ExampleAdapter adapter;

    String url;
    String url2;
    String url3;

    static String urladder = "Transcript.aspx";
    static String urladder2 = "Result.aspx";
    static String urladder3 = "Result_Exam.aspx";


    HashMap&lt;String, String&gt; hashMaps;


    List&lt;String&gt; semester = new ArrayList&lt;&gt;();
    List&lt;Result_Gdata&gt; GPA = new ArrayList&lt;&gt;();
    List&lt;Result_data&gt; data_for_gra = new ArrayList&lt;&gt;();
    List&lt;Result_data&gt; loop_finder = new ArrayList&lt;&gt;();

    List&lt;String&gt; semester2 = new ArrayList&lt;&gt;();
    List&lt;Result_Gdata&gt; GPA2 = new ArrayList&lt;&gt;();
    List&lt;Result_data&gt; data_for_gra2 = new ArrayList&lt;&gt;();
    List&lt;Result_data&gt; loop_finder2 = new ArrayList&lt;&gt;();

    View rootview;

    int num = 0;

    HashMap&lt;String, List&lt;Result_data&gt;&gt; listDataChild;


    FileOutputStream outputStream;
    FileOutputStream outputStream2;

    FileInputStream inputStream;
    FileInputStream inputStream2;

    String filename = "newResultData";
    String filename2 = "oldResultData";

    List&lt;Result_data&gt; data_for_gra3 = null;
    List&lt;String&gt; semesters3 = null;
    List&lt;Result_data&gt; loop_finder3 = null;
    List&lt;Result_Gdata&gt; GPA3 = null;


    List&lt;Result_data&gt; data_for_gra4 = null;
    List&lt;String&gt; semesters4 = null;
    List&lt;Result_data&gt; loop_finder4 = null;
    List&lt;Result_Gdata&gt; GPA4 = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        super.onCreate(savedInstanceState);


        rootview = inflater.inflate(R.layout.anim_result_layout, container, false);

        listView = (AnimatedExpandableListView) rootview.findViewById(R.id.listView);
        hashMaps = Data.map;

        listView.setDivider(null);
        listView.setDividerHeight(0);

       if (Data.mychoice == 1) {
            num = 0;
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    new result_getter_second().execute();
                }
            }, 1800);

        } else {
            Handler handlers2 = new Handler();
            handlers2.postDelayed(new Runnable() {
                @Override
                public void run() {
                    new results().execute();
                }
            }, 2000);
        }
        return rootview;

    }

    private class result_getter_second extends AsyncTask&lt;Void, Void, Void&gt; {


        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            num = 0;

            listDataChild = new HashMap&lt;&gt;();

            List&lt;GroupItem&gt; items = new ArrayList&lt;&gt;();

            if (semester.size() != 0) {
                for (int m = 0; m &lt; semester.size(); m++) {


                    GroupItem item = new GroupItem();
                    item.title = semester.get(m);

                    if (m &lt; GPA.size()) {
                        item.gpa = GPA.get(m).getGPA();
                        item.cgpa = GPA.get(m).getCGPA();
                    }


                    List&lt;Result_data&gt; datw = new ArrayList&lt;&gt;();

                    for (int n = num; n &lt; loop_finder.get(m).getSize_of_the_result(); n++) {
                        ChildItem child = new ChildItem();
                        child.course = data_for_gra.get(n).getSubject();
                        child.grade = data_for_gra.get(n).getGrade();


                        item.items.add(child);


                        datw.add(new Result_data(data_for_gra.get(n).getSubject(), data_for_gra.get(n).getGrade()));
                        num++;
                    }

                    items.add(item);


                    if(getActivity()!=null)
                    {
                        adapter = new ExampleAdapter(getActivity());  // line number 544
                        adapter.setData(items);

                        listView.setAdapter(adapter);
                    }

                    listView.setOnGroupClickListener(new OnGroupClickListener() {

                        @Override
                        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                            if (listView.isGroupExpanded(groupPosition)) {

                                listView.collapseGroupWithAnimation(groupPosition);
                            } else {
                                listView.expandGroupWithAnimation(groupPosition);
                            }
                            return true;
                        }

                    });

                }
            }

        }

    }

    private static class GroupItem {
        String title;
        String gpa;
        String cgpa;
        List&lt;ChildItem&gt; items = new ArrayList&lt;ChildItem&gt;();
    }

    private static class ChildItem {
        String course;
        String grade;
    }

    private static class ChildHolder {
        TextView course;
        TextView grade;
    }

    private static class GroupHolder {
        TextView title;
        TextView gpa;
        TextView cgpa;
    }

    /**
     * Adapter for our list of {@link GroupItem}s.
     */
    private class ExampleAdapter extends AnimatedExpandableListView.AnimatedExpandableListAdapter {
        private LayoutInflater inflater;

        private List&lt;GroupItem&gt; items;

        public ExampleAdapter(Context context) {
            if(context!=null)
            {
                inflater = LayoutInflater.from(context); // line number 886
            }
        }

        public void setData(List&lt;GroupItem&gt; items) {
            this.items = items;
        }

        @Override
        public ChildItem getChild(int groupPosition, int childPosition) {
            return items.get(groupPosition).items.get(childPosition);
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public View getRealChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            ChildHolder holder;
            ChildItem item = getChild(groupPosition, childPosition);
            if (childPosition == 0) {
                holder = new ChildHolder();
                convertView = inflater.inflate(R.layout.anim_list_item_result, parent, false);
                holder.course = (TextView) convertView.findViewById(R.id.textcourse);
                holder.grade = (TextView) convertView.findViewById(R.id.textgrade);
                convertView.setTag(holder);
                holder.course.setText(item.course);
                holder.grade.setText(item.grade);
            } else if (isLastChild) {
                holder = new ChildHolder();
                convertView = inflater.inflate(R.layout.anim_list_item_result_last_child, parent, false);
                holder.course = (TextView) convertView.findViewById(R.id.textcourse);
                holder.grade = (TextView) convertView.findViewById(R.id.textgrade);
                convertView.setTag(holder);
                holder.course.setText(item.course);
                holder.grade.setText(item.grade);
            } else {
                holder = new ChildHolder();
                convertView = inflater.inflate(R.layout.anim_list_item_result_middle, parent, false);
                holder.course = (TextView) convertView.findViewById(R.id.textcourse);
                holder.grade = (TextView) convertView.findViewById(R.id.textgrade);
                convertView.setTag(holder);
                holder.course.setText(item.course);
                holder.grade.setText(item.grade);
            }


            return convertView;
        }

        @Override
        public int getRealChildrenCount(int groupPosition) {
            return items.get(groupPosition).items.size();
        }

        @Override
        public GroupItem getGroup(int groupPosition) {
            return items.get(groupPosition);
        }

        @Override
        public int getGroupCount() {
            return items.size();
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            GroupHolder holder;
            GroupItem item = getGroup(groupPosition);


            if (groupPosition == getGroupCount() - 1) {

                holder = new GroupHolder();
                convertView = inflater.inflate(R.layout.anim_group_item_result_last_child, parent, false);
                holder.title = (TextView) convertView.findViewById(R.id.textTitle);
                holder.gpa = (TextView) convertView.findViewById(R.id.textgpa);
                holder.cgpa = (TextView) convertView.findViewById(R.id.textcgpa);
                convertView.setTag(holder);


                holder.title.setText(item.title);
                holder.gpa.setText(item.gpa);
                holder.cgpa.setText(item.cgpa);
            } else {
                holder = new GroupHolder();
                convertView = inflater.inflate(R.layout.anim_group_item_result, parent, false);
                holder.title = (TextView) convertView.findViewById(R.id.textTitle);
                holder.gpa = (TextView) convertView.findViewById(R.id.textgpa);
                holder.cgpa = (TextView) convertView.findViewById(R.id.textcgpa);
                convertView.setTag(holder);


                holder.title.setText(item.title);
                holder.gpa.setText(item.gpa);
                holder.cgpa.setText(item.cgpa);

            }

            if (groupPosition == 0) {
                ExpandableListView elv = (ExpandableListView) parent;
                elv.expandGroup(groupPosition);
            }

            return convertView;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public boolean isChildSelectable(int arg0, int arg1) {
            return true;
        }

    }

    private boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        //should check null because in air plan mode it will be null
        return (netInfo != null &amp;&amp; netInfo.isConnected());
    }
}
</code></pre>

<p>The problem is in the first line where the <code>getcontext()</code> is called I have tried so much to solve this problem but couldn't. It would be really helpful If you guys help me solve this problem.</p>