title: Start an Activity if string match
body: <p>I am try to implement a function for start an activity only if the name of the item inside a list view match.</p>
  
  <p>This is the code, but on click, the application crash. What's wrong?</p>
  
  <pre><code>lv.setOnItemClickListener(new OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView&lt;?&gt; adapter, View view, int position, long arg) {
                 String selectedValue = adapter.getSelectedItem().toString();
                 if(selectedValue.equalsIgnoreCase("A"))
                 {
                Intent appInfo = new Intent(MainActivity.this, MapActivity.class);
                 startActivity(appInfo);
                 } 
             }
          });
  </code></pre>
  
  <p>and this is the Activity</p>
  
  <pre><code>public class MainActivity extends Activity {
  
  ListView lv;
  ArrayAdapter&lt;MyFile&gt; adapter;
  
  public void list() {
  
      lv = (ListView) findViewById(R.id.listView1);
  
      File dir = new File(Environment.getExternalStorageDirectory().getPath() + "/osmdroid/tiles/");
      File[] filelist = dir.listFiles();
      ArrayList&lt;MyFile&gt; theNamesOfFiles = new ArrayList&lt;MyFile&gt;();
      for (File temp : filelist) {
          theNamesOfFiles.add(new MyFile(temp));
      }
      Collections.sort(theNamesOfFiles, new Comparator&lt;MyFile&gt;() {
  
          @Override
          public int compare(MyFile lhs, MyFile rhs) {
              return lhs.toString().compareToIgnoreCase(rhs.toString());
          }
      });
      adapter = new ArrayAdapter&lt;MyFile&gt;(this, R.layout.list_row,theNamesOfFiles);
      lv.setAdapter(adapter);
  }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      list();
  
  
      lv.setOnItemClickListener(new OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView&lt;?&gt; adapter, View view, int position, long arg) {
                 String selectedValue = adapter.getSelectedItem().toString();
                 if(selectedValue.equalsIgnoreCase("A"))
                 {
                Intent appInfo = new Intent(MainActivity.this, MapActivity.class);
                 startActivity(appInfo);
                 } 
             }
          });
  
  
      lv.setOnItemLongClickListener(new OnItemLongClickListener() {
          // setting onItemLongClickListener and passing the position to the
          // function
          @Override
          public boolean onItemLongClick(AdapterView&lt;?&gt; arg0, View arg1,
                  final int position, long arg3) {
  
              AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
              builder.setMessage("Delete Map ?");
              builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
  
                 @Override
                 public void onClick(DialogInterface dialog, int which) {
                     doDeleteFile(adapter.getItem(position).file);
                      dialog.dismiss();
                 }
              });
  
              builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
  
                 @Override
                 public void onClick(DialogInterface dialog, int which) {
  
                      dialog.dismiss();
                 }
  
              });
  
  
              AlertDialog alert = builder.create();
              alert.show();
  
  
  
              return true;
          }
      });
  }
  
  
  
  
  public void doDeleteFile(File fileToDelete) {
      if (!fileToDelete.isDirectory()) {
  
          try {
              if (fileToDelete.delete()) {
  
                  System.out.println("File delete operation success");
                  list();
              }
  
              else {
                  System.out.println("File delete operation failed");
              }
          } catch (Exception ex) {
          System.out.println("Exception :" + ex.getMessage());
          }
  
      } else {
  
          System.out.println("It is  not a file");
  
      }
  
  }
  
  public class MyFile {
      File file;
      String fileName;
  
      public MyFile(File file)
      {
          this.file = file;
          this.fileName = file.getName();
          fileName = fileName.substring(0, fileName.lastIndexOf('.'));
      }
  
      @Override
      public String toString() {
          return this.fileName;
      }
  }
  }
  </code></pre>
  
  <p>This is the stack trace</p>
  
  <pre><code>03-13 19:59:36.998: E/AndroidRuntime(14188): FATAL EXCEPTION: main
  03-13 19:59:36.998: E/AndroidRuntime(14188): java.lang.NullPointerException
  03-13 19:59:36.998: E/AndroidRuntime(14188):    at com.example.deletelistitem.MainActivity$2.onItemClick(MainActivity.java:60)
  03-13 19:59:36.998: E/AndroidRuntime(14188):    at android.widget.AdapterView.performItemClick(AdapterView.java:301)
  03-13 19:59:36.998: E/AndroidRuntime(14188):    at android.widget.AbsListView.performItemClick(AbsListView.java:1490)
  03-13 19:59:36.998: E/AndroidRuntime(14188):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3275)
  03-13 19:59:36.998: E/AndroidRuntime(14188):    at android.widget.AbsListView$1.run(AbsListView.java:4518)
  03-13 19:59:36.998: E/AndroidRuntime(14188):    at android.os.Handler.handleCallback(Handler.java:725)
  03-13 19:59:36.998: E/AndroidRuntime(14188):    at android.os.Handler.dispatchMessage(Handler.java:92)
  03-13 19:59:36.998: E/AndroidRuntime(14188):    at android.os.Looper.loop(Looper.java:137)
  03-13 19:59:36.998: E/AndroidRuntime(14188):    at android.app.ActivityThread.main(ActivityThread.java:5283)
  03-13 19:59:36.998: E/AndroidRuntime(14188):    at java.lang.reflect.Method.invokeNative(Native Method)
  03-13 19:59:36.998: E/AndroidRuntime(14188):    at java.lang.reflect.Method.invoke(Method.java:511)
  03-13 19:59:36.998: E/AndroidRuntime(14188):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
  03-13 19:59:36.998: E/AndroidRuntime(14188):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
  03-13 19:59:36.998: E/AndroidRuntime(14188):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>This is the stack trace if i use  String selectedItem = lv.getSelectedItem().toString();</p>
  
  <pre><code>03-13 21:39:57.381: E/AndroidRuntime(22701): FATAL EXCEPTION: main
  03-13 21:39:57.381: E/AndroidRuntime(22701): java.lang.NullPointerException
  03-13 21:39:57.381: E/AndroidRuntime(22701):    at com.example.deletelistitem.MainActivity$2.onItemClick(MainActivity.java:60)
  03-13 21:39:57.381: E/AndroidRuntime(22701):    at android.widget.AdapterView.performItemClick(AdapterView.java:301)
  03-13 21:39:57.381: E/AndroidRuntime(22701):    at android.widget.AbsListView.performItemClick(AbsListView.java:1490)
  03-13 21:39:57.381: E/AndroidRuntime(22701):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3275)
  03-13 21:39:57.381: E/AndroidRuntime(22701):    at android.widget.AbsListView$1.run(AbsListView.java:4518)
  03-13 21:39:57.381: E/AndroidRuntime(22701):    at android.os.Handler.handleCallback(Handler.java:725)
  03-13 21:39:57.381: E/AndroidRuntime(22701):    at android.os.Handler.dispatchMessage(Handler.java:92)
  03-13 21:39:57.381: E/AndroidRuntime(22701):    at android.os.Looper.loop(Looper.java:137)
  03-13 21:39:57.381: E/AndroidRuntime(22701):    at android.app.ActivityThread.main(ActivityThread.java:5283)
  03-13 21:39:57.381: E/AndroidRuntime(22701):    at java.lang.reflect.Method.invokeNative(Native Method)
  03-13 21:39:57.381: E/AndroidRuntime(22701):    at java.lang.reflect.Method.invoke(Method.java:511)
  03-13 21:39:57.381: E/AndroidRuntime(22701):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
  03-13 21:39:57.381: E/AndroidRuntime(22701):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
  03-13 21:39:57.381: E/AndroidRuntime(22701):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>I solve the problem using this:</p>
  
  <pre><code>String selected;
  selected = lv.getItemAtPosition(position).toString();
  </code></pre>