title: Spinner in Dialog - NullPointerException - Android
body: <p>I want to show a custom dialog with a spinner in it. But when i do I get NullPointerException at the setAdapter() method.I have been trying for over a week now and couldnt figure out how to get this right. here's my code: </p>
  
  <pre><code>     AlertDialog alertDialog;
  
       LayoutInflater inflater = 
                   (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);                          
       View layout = inflater.inflate(R.layout.form,
                                      (ViewGroup) findViewById(R.id.layout_root));
  
       ArrayAdapter&lt;String&gt; spinnerAdapter = new ArrayAdapter&lt;String&gt;(this,
             android.R.layout.simple_spinner_item, new String[] {"0","1","2"});
       Spinner spinner = (Spinner) findViewById(R.id.spinner1);
  
      //I get the error in the following line:
  
      try{ 
  
           spinner.setAdapter(spinnerAdapter);
  
      }catch(Exception exception){
           Toast.makeText(getApplicationContext(),
      "Exception: "+exception,Toast.LENGTH_SHORT).show();
        }
  
       AlertDialog.Builder  builder = new AlertDialog.Builder(this);
       builder.setView(layout);
       alertDialog = builder.create();
       alertDialog.setTitle("Security");
       alertDialog.show();
     }
  </code></pre>
  
  <p>Here's the xml file form.xml:</p>
  
  <pre><code>  ?xml version="1.0" encoding="utf-8"?&gt;
  
  
    &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical"
     android:id="@+id/layout_root" &gt;
  
  
     &lt;Spinner
      android:id="@+id/spinner1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" /&gt;
  
    &lt;/LinearLayout&gt; 
  </code></pre>
  
  <p>Please help me out. I've followed the link : <a href="https://stackoverflow.com/questions/6365268/spinner-in-dialog-nullpointerexception">Spinner in dialog - NullPointerException</a>
  which discusses the same problem but i still couldnt do it.</p>