title: Populate the spinner from JSON in Android
body: <p>I have JSON data like this: </p>
  
  <pre><code>[{"id":"1",
  "client_name":"Glasco Smith Klin",
  "campaign_name":"Health Medicine Survay",
  "question":"MODE OF COMPLAINT CAN BE GIVEN THROUGH __________________",
  "option_A":"WRITTEN",
  "option_B":"ORAL",
  "option_C":"PHONE CALL",
  "option_D":"ALL OF THE ABOVE",
  "q_prefix":"GSKHMSQ"},
  
  {"id":"2",
  "client_name":"Glasco Smith Klin",
  "campaign_name":"Health Medicine Survay",
  "question":"ADDING NEW FIR CAN BE DONE FROM WHICH ROLE",
  "option_A":"IO",
  "option_B":"TRAFFIC",
  "option_C":"SHO-CRIME",
  "option_D":"ADMINISTRATOR",
  "q_prefix":"GSKHMJD"}]
  </code></pre>
  
  <p>I want to retrieve all <code>q_prefix</code> column into spinner. But in my spinner no data is displayed. When I run my app, I am getting an error: "Unable to start Activity" and <code>Java.lang.NullPointerException</code> at this line: <code>jsonArray = jSon.getJSONArray(TAG_CODE);</code>.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public class SpinDemo extends Activity {
  
      private static String strUrl = "http://192.168.1.61/jyoti/android_app/all_questions.php";
      private static String TAG_ID = "id";
      private static String TAG_CODE = "q_prefix";
      JSONArray jsonArray = null;
      Spinner codeSpinner, spinner2;
  
      @SuppressWarnings("unused")
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
  
          codeSpinner = (Spinner) findViewById(R.id.spinner2);
  
          ArrayList&lt;String&gt; codeList = new ArrayList&lt;String&gt;();
  
          // creating instance of JSONPrser
          JSONParser jParser = new JSONParser();
          // getting JSON string from URL
          JSONObject jSon = jParser.getJSONFromUrl(strUrl);
          System.out.println("Hello ********************");
  
          try {
              // Getting Array of Code
              jsonArray = jSon.getJSONArray(TAG_CODE);
              final String[] items = new String[jsonArray.length()];
              // looping through All Contacts
              for (int i = 0; i &lt; jsonArray.length(); i++) {
                  JSONObject c = jsonArray.getJSONObject(i);
  
                  // Storing each json item in variable
                  String strCode = c.getString(TAG_CODE);
                  items[i] = c.getString(TAG_CODE);
                  System.out.println("Hello events " + items);
              }
  
              ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this,
                      android.R.layout.simple_spinner_item);
              adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
              codeSpinner.setAdapter(adapter);
          } catch (JSONException je) {
              je.printStackTrace();
  
          }
  
      }
  
  }
  </code></pre>