Q:

How to refer a Spinner entry correctly?

I've been making an app for Android which uses a Spinner list to show the name of a few cities. I need the program to change the TextView right below the spinner to display the location of the selected item in the array. I wrote the following code for the same
public class MainScreen extends Activity {
String[] cityArray = { "Agra ", "Ahmedabad", "Alappuzha", "Amritsar"};
Spinner list = (Spinner) findViewById(R.id.ListView1);
TextView nam = (TextView)findViewById(R.id.textView2);

@Override
protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main_screen);

      ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, cityArray);
      list.setAdapter(adapter);

list.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parentView, View view,int position, long id) 
    {
        nam.setText(list.getSelectedItemPosition());
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        nam.setText("None Selected");
    }

});
}
}

There are no errors while running the program, but the app simply force closes as soon as it is opened.
Logcat:
04-10 08:49:30.423: I/dalvikvm(561): threadid=3: reacting to signal 3
04-10 08:49:30.723: I/dalvikvm(561): Wrote stack traces to '/data/anr/traces.txt'
04-10 08:49:30.783: I/dalvikvm(561): threadid=3: reacting to signal 3
04-10 08:49:30.823: D/AndroidRuntime(561): Shutting down VM
04-10 08:49:30.823: W/dalvikvm(561): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-10 08:49:30.844: E/AndroidRuntime(561): FATAL EXCEPTION: main
04-10 08:49:30.844: E/AndroidRuntime(561): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.droidrish.droidweather/com.droidrish.droidweather.MainScreen}: java.lang.NullPointerException
04-10 08:49:30.844: E/AndroidRuntime(561):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
04-10 08:49:30.844: E/AndroidRuntime(561):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-10 08:49:30.844: E/AndroidRuntime(561):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-10 08:49:30.844: E/AndroidRuntime(561):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-10 08:49:30.844: E/AndroidRuntime(561):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-10 08:49:30.844: E/AndroidRuntime(561):  at android.os.Looper.loop(Looper.java:137)
04-10 08:49:30.844: E/AndroidRuntime(561):  at android.app.ActivityThread.main(ActivityThread.java:4424)
04-10 08:49:30.844: E/AndroidRuntime(561):  at java.lang.reflect.Method.invokeNative(Native Method)
04-10 08:49:30.844: E/AndroidRuntime(561):  at java.lang.reflect.Method.invoke(Method.java:511)
04-10 08:49:30.844: E/AndroidRuntime(561):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-10 08:49:30.844: E/AndroidRuntime(561):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-10 08:49:30.844: E/AndroidRuntime(561):  at dalvik.system.NativeStart.main(Native Method)
04-10 08:49:30.844: E/AndroidRuntime(561): Caused by: java.lang.NullPointerException
04-10 08:49:30.844: E/AndroidRuntime(561):  at android.app.Activity.findViewById(Activity.java:1794)
04-10 08:49:30.844: E/AndroidRuntime(561):  at com.droidrish.droidweather.MainScreen.<init>(MainScreen.java:15)
04-10 08:49:30.844: E/AndroidRuntime(561):  at java.lang.Class.newInstanceImpl(Native Method)
04-10 08:49:30.844: E/AndroidRuntime(561):  at java.lang.Class.newInstance(Class.java:1319)
04-10 08:49:30.844: E/AndroidRuntime(561):  at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
04-10 08:49:30.844: E/AndroidRuntime(561):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
04-10 08:49:30.844: E/AndroidRuntime(561):  ... 11 more
04-10 08:49:30.844: I/dalvikvm(561): Wrote stack traces to '/data/anr/traces.txt'
04-10 08:49:31.416: I/dalvikvm(561): threadid=3: reacting to signal 3
04-10 08:49:31.443: I/dalvikvm(561): Wrote stack traces to '/data/anr/traces.txt'
04-10 08:49:31.583: I/dalvikvm(561): threadid=3: reacting to signal 3
04-10 08:49:31.603: I/dalvikvm(561): Wrote stack traces to '/data/anr/traces.txt'
04-10 08:49:33.722: I/Process(561): Sending signal. PID: 561 SIG: 9

Am I using the OnItemSelected Listener the right way? I'm expecting the program to change the text of the TextView to the location of the item in the Spinner.

A:

Spinner and textview should be initialized inside onCreate like this:
public class MainScreen extends Activity {

      String[] cityArray = { "Agra ", "Ahmedabad", "Alappuzha", "Amritsar"};
      Spinner list;
      TextView nam;

@Override
protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main_screen);

      list = (Spinner)findViewById(R.id.ListView1);
      nam = (TextView)findViewById(R.id.textView2);

And then change this:
 nam.setText(list.getSelectedItemPosition());

to:
nam.setText(list.getSelectedItem().toString());

If you just want the index, do this:
nam.setText(String.valueOf(list.getSelectedItemPosition()));

