title: Java NullPointerException on Android Spinner
body: <p>i have a spinner an two buttons on my Android app,</p>
  
  <p>the spinner is populated dynamicaly from server-side script.</p>
  
  <p>it works last time, but i don't know why now i'm having a java.NullPointerException problem and i don't know how to fix it, the log from LogCat said that i'm having null on my spinner</p>
  
  <p>first, this is my complete script on <strong>FormPilihLokasiKelompok.class</strong></p>
  
  <pre><code>package com.nigmagrid.jm.demo;
  
  import java.util.ArrayList;
  
  import android.app.Activity;
  import android.app.ProgressDialog;
  import android.content.Context;
  import android.content.Intent;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.Button;
  import android.widget.Spinner;
  import android.widget.Toast;
  import android.widget.AdapterView.OnItemSelectedListener;
  
  public class FormPilihLokasiKelompok extends Activity {
  
      ArrayList&lt;String&gt; content;
      ArrayAdapter&lt;String&gt; content_adapter;
      Button btn_save, btn_back;
      Context context;
      String selected_value, spinner_prompt, toast_failed, url_retrieval;
      Spinner spinner;
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.form_pilih_lokasi_kelompok);
  
          btn_save = (Button) findViewById(R.id.btn_save);
          btn_back = (Button) findViewById(R.id.btn_back);
          spinner = (Spinner) findViewById(R.id.spinner);
  
          context = FormPilihLokasiKelompok.this;
  
          selected_value = "";
          spinner_prompt = "Pilih Lokasi";
          toast_failed = "Tidak ada lokasi yang terpilih";
  
          url_retrieval = VarsUrl.getServerAddress()+"get_list.php?table=kelompok_lokasi";
  
          content = new ArrayList&lt;String&gt;();
  
          btn_back.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View arg0) {
                  finish();
              }
          });
  
          btn_save.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if(selected_value.length() &gt; 1){
                      Intent intent = new Intent(getApplicationContext(), FormPilihLokasiKm.class);
                      intent.putExtra("content", selected_value);
                      startActivity(intent);
                  }else{
                      Toast.makeText(getApplicationContext(), toast_failed, Toast.LENGTH_SHORT).show();
                  }
              }
          });
  
          spinner.setPrompt(spinner_prompt);
          spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
  
              @Override
              public void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) {
                  String sel_val = spinner.getSelectedItem().toString();
                  if(sel_val != null || sel_val.length() &gt; 0){
                      selected_value = spinner.getSelectedItem().toString();
                  }
              }
  
              @Override
              public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
                  // do nothing
              }
  
          });
  
          new PopulateSpinner().execute(url_retrieval);
  
      }
  
      protected class PopulateSpinner extends AsyncTask&lt;String, Void, String&gt;{
  
          ProgressDialog pd = new ProgressDialog(context);
  
          protected void onPreExecute(){
              pd.setMessage("Mengambil data...");
          }
  
          @Override
          protected String doInBackground(String... arg0) {
              try{
                  String response = CustomHttpClient.excecuteHttpGet(arg0[0]);
                  String res = response.toString().trim();
                  return res;             
              }catch (Exception e){
                  return e.toString();
              }
          }
  
          protected void onPostExecute(String res){
              String[] datanya = res.split("#", 0);
              int jumlah_data = datanya.length;
              if(datanya[0].equals("1")){
                  try{
                      content.clear();
                      for(int i = 1; i &lt; jumlah_data; i++){
                          content.add(datanya[i]);
                      }
  
                      content_adapter = new ArrayAdapter&lt;String&gt;(context, android.R.layout.simple_spinner_item, content);
                      content_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                      spinner.setAdapter(content_adapter);
  
                  }catch(Exception e){
                      Toast.makeText(getApplicationContext(), "Terjadi kesalahan sistem\n"+e.toString(), Toast.LENGTH_SHORT).show();
                  }
              }else{
                  content.add("Tidak ada data untuk ditampilkan");                
              }
  
              content_adapter = new ArrayAdapter&lt;String&gt;(context, android.R.layout.simple_spinner_item, content);
              content_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
              spinner.setAdapter(content_adapter);
  
              pd.dismiss();
          }
      }
  
  }
  </code></pre>
  
  <p>Here's the Log</p>
  
  <pre><code>11-23 12:49:57.179: W/dalvikvm(22610): threadid=1: thread exiting with uncaught exception (group=0x40015578)
  11-23 12:49:57.187: E/AndroidRuntime(22610): FATAL EXCEPTION: main
  11-23 12:49:57.187: E/AndroidRuntime(22610): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nigmagrid.jm.demo/com.nigmagrid.jm.demo.FormPilihLokasiKelompok}: java.lang.NullPointerException
  11-23 12:49:57.187: E/AndroidRuntime(22610):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
  11-23 12:49:57.187: E/AndroidRuntime(22610):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
  11-23 12:49:57.187: E/AndroidRuntime(22610):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
  11-23 12:49:57.187: E/AndroidRuntime(22610):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
  11-23 12:49:57.187: E/AndroidRuntime(22610):    at android.os.Handler.dispatchMessage(Handler.java:99)
  11-23 12:49:57.187: E/AndroidRuntime(22610):    at android.os.Looper.loop(Looper.java:123)
  11-23 12:49:57.187: E/AndroidRuntime(22610):    at android.app.ActivityThread.main(ActivityThread.java:3687)
  11-23 12:49:57.187: E/AndroidRuntime(22610):    at java.lang.reflect.Method.invokeNative(Native Method)
  11-23 12:49:57.187: E/AndroidRuntime(22610):    at java.lang.reflect.Method.invoke(Method.java:507)
  11-23 12:49:57.187: E/AndroidRuntime(22610):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
  11-23 12:49:57.187: E/AndroidRuntime(22610):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
  11-23 12:49:57.187: E/AndroidRuntime(22610):    at dalvik.system.NativeStart.main(Native Method)
  11-23 12:49:57.187: E/AndroidRuntime(22610): Caused by: java.lang.NullPointerException
  11-23 12:49:57.187: E/AndroidRuntime(22610):    at com.nigmagrid.jm.demo.FormPilihLokasiKelompok.onCreate(FormPilihLokasiKelompok.java:68)
  11-23 12:49:57.187: E/AndroidRuntime(22610):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  11-23 12:49:57.187: E/AndroidRuntime(22610):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
  11-23 12:49:57.187: E/AndroidRuntime(22610):    ... 11 more
  </code></pre>
  
  <p>this script was on line 68 : spinner.setPrompt(spinner_prompt);</p>
  
  <p>i've change the spinner_prompt to normal string like "test prompt" but it's still same
  so it must be the spinner (cmiiw) and i don't know where i should fix it.</p>
  
  <p>anybody knows how to fix it?
  thanks</p>
  
  <p>update
  here's my layout</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;ScrollView
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"&gt;
  
      &lt;LinearLayout
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"&gt;
  
          &lt;TextView 
              android:text="Lokasi"
              android:padding="5dip"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content" /&gt;
          &lt;Spinner
              android:id="@+id/spinner"
              android:padding="5dip"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content" /&gt;
          &lt;Button
              android:id="@+id/btn_save"
              android:text="Lanjutkan"
              android:padding="5dip"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content" /&gt;
  
          &lt;Button
              android:id="@+id/btn_back"
              android:text="Kembali"
              android:padding="5dip"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content" /&gt;
  
      &lt;/LinearLayout&gt;
  &lt;/ScrollView&gt;
  </code></pre>