title: Update ListView adapter in a fragment from outside the onCreateView method
body: <p>I'm trying to build an app that populates ListView from JSON recievced. I'm getting the below exception when i try to update the listview using notifyDataSetChanged. </p>
  
  <blockquote>
    <p>Attempt to invoke virtual method 'void
    com.example.android.Wordtrend.CustomArrayAdapter.notifyDataSetChanged()'
    on a null object reference</p>
  </blockquote>
  
  <p>The ListView is inside a fragment. Im using the onPostExecute method to call the update method inside the fragment to update the ArrayAdapter. UI loads the  dummy data provided in the onCreateView method but fails during updation.</p>
  
  <p>Im a newbie, so any advice would be greatly helpful. Im confused because ArrayAdapter is null and the size of dataArray is zero inside the update method. </p>
  
  <p><strong>Activity:</strong></p>
  
  <p>public class ClassicActivity extends AppCompatActivity {</p>
  
  <pre><code>    @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activitycategory);
          getSupportFragmentManager().beginTransaction().replace(R.id.container,new ClassicFragment()).commit();
      }       
  }
  </code></pre>
  
  <p><strong>Fragment:</strong></p>
  
  <pre><code>public class ClassicFragment extends Fragment {
      CustomArrayAdapter adapter;
      ArrayList&lt;dataobjects&gt; dataArray=new ArrayList&lt;&gt;();
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          dataArray.clear();
          dataArray.add(new dataobjects("word","Explanation",R.drawable.placeholder,R.raw.color_black));
          dataArray.add(new dataobjects("word","Explanation",R.drawable.placeholder,R.raw.color_black));
          dataArray.add(new dataobjects("word","Explanation",R.drawable.placeholder,R.raw.color_black));
  
          View rootView=inflater.inflate(R.layout.word_list,container,false);
          ListView listView=(ListView)rootView.findViewById(R.id.list);
          adapter=new CustomArrayAdapter(getActivity(),dataArray,R.color.classic);
          listView.setAdapter(adapter);
          return rootView;
      }
  
  
        public void update(ArrayList&lt;FinalData&gt; e){
           ArrayList&lt;dataobjects&gt; newDataArray=new ArrayList&lt;&gt;();  // update Adapter from JSON 
           for(int i=0;i&lt;e.size();i++) {
             FinalData FinalDataObject = e.get(i);
             String word=FinalDataObject.getWord();
             String meaning=FinalDataObject.getMeaning();
             newDataArray.add(new dataobjects(word,meaning,R.drawable.placeholder,R.raw.color_black));         
            }
  
   Log.d("Classic"," Size of Data"+dataArray.size());   // Shows zero but im not clearing it manually. Should be two.
  
  
               dataArray.addAll(newDataArray);  // Size changes to 7 as expected.
                 adapter.notifyDataSetChanged();
  
          }
  </code></pre>
  
  <p>// Calling update method from the class Dataprocess </p>
  
  <pre><code>public class Data extends AsyncTask&lt;String,Void,String&gt; {
  
          String Mdata;
          @Override
          protected void onPostExecute(String s) {
              Mdata=s;
              Dataprocess d=new Dataprocess();
              d.process(Mdata);
              super.onPostExecute(s);
          }.....}
  public class Dataprocess {
  
      Finaldata f;
      ArrayList&lt;Finaldata&gt; wordList=new ArrayList&lt;&gt;();
      ClassicFragment classicfragment=new ClassicFragment();
      public void process(String s ){
          if(s!=null){
              try {
                  JSONObject data = new JSONObject(s);
                  JSONArray word = data.getJSONArray("WordArray");
                  for (int i = 0; i &lt; word.length(); i++){
                      JSONObject c = word.getJSONObject(i);
                      String word1=c.getString("word");
                      String meaning=c.getString("meaning");
                      f=new Finaldata(word1,meaning);
                      wordList.add(f);
                  }
                  classicfragment.update(wordList);
              }catch (Exception e){
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>CustomArray Adapter:</strong></p>
  
  <pre><code>    public class CustomArrayAdapter extends ArrayAdapter&lt;dataobjects&gt; {
          int color;
          public  static MediaPlayer mediaPlayer;
          static AudioManager am;
          private TextToSpeech tts;
  
          static MediaPlayer.OnCompletionListener oncom=new MediaPlayer.OnCompletionListener() {
              @Override
              public void onCompletion(MediaPlayer mp) {
                  release();
              }
          };
  
          public CustomArrayAdapter(Activity context, ArrayList&lt;dataobjects&gt; a, int color) {
              super(context,0,a);
              this.color=color;
          }
  
          @Override
  
          public View getView(int position, View convertView, ViewGroup parent) {
  
              View listitemview=convertView;
              if(listitemview==null){
                  listitemview= LayoutInflater.from(getContext()).inflate(R.layout.customlayout,parent,false);
              }
              dataobjects currentword=getItem(position);
              TextView t1=(TextView)listitemview.findViewById(R.id.textView);
              TextView t2=(TextView)listitemview.findViewById(R.id.textView2);
              View background1=listitemview.findViewById(R.id.back);
              View background2=listitemview.findViewById(R.id.back2);
              background1.setBackgroundColor(colorid);
              background2.setBackgroundColor(colorid);
              int ex=currentword.getImageURL();
              int aud=currentword.getSound();
              t1.setText(currentword.getWord());
              t2.setText(currentword.getTransalation());
              int colorid= ContextCompat.getColor(getContext(),color);
  
              ImageView im1=(ImageView)listitemview.findViewById(R.id.media);
  
              im1.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
  
                       test();
  
                  }
              });
  
              return listitemview;
  
          }
  </code></pre>