title: How to add strings from alert-dialog box in android?
body: <p>I am working with android. Now i got a problem with it. I created a progrm that will get the strings from user via an alert-dialog box. And add it into the spinner. But it shows some exceptions in logcat. Please any one help me to fix this.Here is my code</p>
  
  <pre><code>**MainActivity.java**
  
  package com.example.picker;
  
  import java.util.ArrayList;
  
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.os.Bundle;
  import android.text.Editable;
  import android.util.Log;
  import android.view.Menu;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Spinner;
  
  public class MainActivity extends Activity {
     Button add_button;
  
     Editable value;
     String inserted_value;
     AlertDialog.Builder alert;
     Spinner spinner;
  
     /** Items entered by the user is stored in this ArrayList variable */
     ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
  
     /** Declaring an ArrayAdapter to set items to ListView */
     ArrayAdapter&lt;String&gt; adapter;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          add_button = (Button)findViewById(R.id.addbutton);
          alert = new AlertDialog.Builder(this);
          final EditText input = new EditText(this);
  
           /** Getting a reference to Spinner object of the resource activity_main */
          spinner = (Spinner) findViewById(R.id.spin1);
  
          add_button.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
  
                  alert.setTitle("Picker");
                  alert.setMessage("Enter a string...");
  
                  // Set an EditText view to get user input 
  
                  alert.setView(input);
  
                  alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int whichButton) {
  if(inserted_value !=null){
  
                         Log.d("GOT_TEXT",inserted_value);
                         list.add(input.getText().toString());
                       }
                      else{
                          Log.d("GOT_TEXT","inserted_value is null"); 
                       }
  
  
                      /** Setting the adapter to the ListView */
                      spinner.setAdapter(adapter);
  
                      /** Adding radio buttons for the spinner items */
                      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  
                    }
                  });
  
                  alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                      // Canceled.
                        dialog.dismiss();
                    }
                  });
  
                  alert.show();
  
              }
          });
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.activity_main, menu);
          return true;
      }
  
  }
  </code></pre>
  
  <p><strong>logcat</strong></p>
  
  <pre><code>    01-28 13:45:56.138: E/AndroidRuntime(701): FATAL EXCEPTION: main
  01-28 13:45:56.138: E/AndroidRuntime(701): java.lang.NullPointerException
  01-28 13:45:56.138: E/AndroidRuntime(701):  at com.example.picker.MainActivity$1$1.onClick(MainActivity.java:76)
  01-28 13:45:56.138: E/AndroidRuntime(701):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
  01-28 13:45:56.138: E/AndroidRuntime(701):  at android.os.Handler.dispatchMessage(Handler.java:99)
  01-28 13:45:56.138: E/AndroidRuntime(701):  at android.os.Looper.loop(Looper.java:123)
  01-28 13:45:56.138: E/AndroidRuntime(701):  at android.app.ActivityThread.main(ActivityThread.java:4627)
  01-28 13:45:56.138: E/AndroidRuntime(701):  at java.lang.reflect.Method.invokeNative(Native Method)
  01-28 13:45:56.138: E/AndroidRuntime(701):  at java.lang.reflect.Method.invoke(Method.java:521)
  01-28 13:45:56.138: E/AndroidRuntime(701):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
  01-28 13:45:56.138: E/AndroidRuntime(701):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
  01-28 13:45:56.138: E/AndroidRuntime(701):  at dalvik.system.NativeStart.main(Native Method)
  </code></pre>