title: Android-Populate a spinner using asynctask by json response from the server (NullPointerException: Attempt to invoke virtual method ..)
body: <p>I am trying to populate a Spinner from a JSON response using an async task. I am getting the JSON object as a server response and the response is correct. Here it is.
  <code>[{"category_id":1,"category_name":"History"},{"category_id":2,"category_name":"Sports"},{"category_id":3,"category_name":"Science"},{"category_id":4,"category_name":"gg"}]</code></p>
  
  <p>I am calling an asynctask on the <code>View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstranceState)</code></p>
  
  <p>But I am getting a 'null object reference error' on HomeFragment activity launch. </p>
  
  <p>Here is my SpinnerAdapter.class.</p>
  
  <pre><code>package com.example.quizapp;
  
  import android.content.Context;
  import android.support.v7.widget.RecyclerView;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.ListAdapter;
  import android.widget.TextView;
  
  import com.example.quizapp.model.Category;
  
  import java.util.List;
  
  public class SpinnerAdapter extends BaseAdapter
  {
      private LayoutInflater layoutInflater;
      private List&lt;Category&gt; categoryList;
      private Context context;
  
  public SpinnerAdapter(Context context, List&lt;Category&gt; list){
      this.context = context;
      layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      this.categoryList = list;
  }
  
  @Override
  public int getCount() {
      return categoryList.size();
  }
  
  @Override
  public Object getItem(int position) {
      return (Category)categoryList.get(position);
  }
  
  @Override
  public long getItemId(int i) {
      return 0;
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      ViewHolder spinnerHolder;
      if(convertView == null){
          spinnerHolder = new ViewHolder();
          convertView = layoutInflater.inflate(R.layout.category_spinner_text_layout, parent,false);
          spinnerHolder.spinnerItemList = (TextView) convertView.findViewById(R.id.catsSpinnerText);
          convertView.setTag(spinnerHolder);
      }else {
          spinnerHolder = (ViewHolder) convertView.getTag();
      }
      spinnerHolder.spinnerItemList.setText(categoryList.get(position).getCatName());
      return convertView;
  }
  
  class ViewHolder{
      TextView spinnerItemList;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is the AsyncTask for populating the Spinner written in the HomeFragment.class.</p>
  
  <pre><code>public class CategoryLoadAsyncTask extends AsyncTask&lt;Void, String, Category[]&gt; {
  
      @Override
      protected void onPreExecute(){
          progressDialog = ProgressDialog.show(getActivity(), "Loading Game", "Please wait..");
      }
  
      @Override
      protected Category[] doInBackground(Void... params) {
          String json ="";
          Category[] categories = null;
          try {
              json = new RestClient().doGet(getString(R.string.category_url));
              categories = new Gson().fromJson(json, Category[].class);
          } catch (Exception e) {
              e.printStackTrace();
          }
          return categories;
      }
  
      @Override
      protected void onPostExecute(Category[] categories){
          progressDialog.dismiss();
          if (categories != null){
              SpinnerAdapter spinnerAdapter = new SpinnerAdapter(context, Arrays.asList(categories));
              catsSpinner.setAdapter(spinnerAdapter);
          }
      }
  }
  </code></pre>
  
  <p>Here is the error log.</p>
  
  <p><code>E/AndroidRuntime: FATAL EXCEPTION: main
                    Process: com.example.quizapp, PID: 2381
                    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference
                        at com.example.quizapp.HomeFragment$CategoryLoadAsyncTask.onPostExecute(HomeFragment.java:90)
                        at com.example.quizapp.HomeFragment$CategoryLoadAsyncTask.onPostExecute(HomeFragment.java:64)
                        at android.os.AsyncTask.finish(AsyncTask.java:660)
                        at android.os.AsyncTask.-wrap1(AsyncTask.java)
                        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
                        at android.os.Handler.dispatchMessage(Handler.java:102)
                        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:866)
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)</code></p>
  
  <p>Can anybody help me with this?</p>