Android substring force closes
<p>I give up. I use substring a lot but this time I cannot make it work. I am selecting a contact from a spinner (that is populated with the contacts of the phone) and I want the surname of that selected contact. Part of the code:</p>

<p>public class MyOnItemSelectedListener implements OnItemSelectedListener {</p>

<pre><code>public void onItemSelected(AdapterView&lt;?&gt; parent,
    View view, int pos, long id) {
    selectedname = parent.getItemAtPosition(pos).toString();
    sn = selectedname .indexOf(' ');
    String selectedname2= selectedname.substring(1, 3); //force close line
}

public void onNothingSelected(AdapterView parent) {

}
</code></pre>

<p>}</p>

<p>The selectedname and sn variables return real values.
e.g. the</p>

<pre><code>    BtnOK = (Button)findViewById(R.id.Button01);
BtnOK.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Toast.makeText(Alarm.this, "selname: " + selectedname + "-" + sn, Toast.LENGTH_LONG).show();

    }
});
</code></pre>

<p>returns selname: Anna-4</p>

<p>But if i include the <code>String selectedname2= selectedname.substring(1, 3);</code> line, it force closes.
Any ideas?</p>

<p>Logcat:</p>

<pre><code>07-04 17:05:32.140: ERROR/AndroidRuntime(280): FATAL EXCEPTION: main
07-04 17:05:32.140: ERROR/AndroidRuntime(280): java.lang.StringIndexOutOfBoundsException
07-04 17:05:32.140: ERROR/AndroidRuntime(280):     at java.lang.String.substring(String.java:1579)
07-04 17:05:32.140: ERROR/AndroidRuntime(280):     at com.bfarago.app.Alarm$MyOnItemSelectedListener.onItemSelected(Alarm.java:577)
07-04 17:05:32.140: ERROR/AndroidRuntime(280):     at android.widget.AdapterView.fireOnSelected(AdapterView.java:864)
07-04 17:05:32.140: ERROR/AndroidRuntime(280):     at android.widget.AdapterView.access$200(AdapterView.java:42)
07-04 17:05:32.140: ERROR/AndroidRuntime(280):     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:830)
07-04 17:05:32.140: ERROR/AndroidRuntime(280):     at android.os.Handler.handleCallback(Handler.java:587)
07-04 17:05:32.140: ERROR/AndroidRuntime(280):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-04 17:05:32.140: ERROR/AndroidRuntime(280):     at android.os.Looper.loop(Looper.java:123)
07-04 17:05:32.140: ERROR/AndroidRuntime(280):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-04 17:05:32.140: ERROR/AndroidRuntime(280):     at java.lang.reflect.Method.invokeNative(Native Method)
07-04 17:05:32.140: ERROR/AndroidRuntime(280):     at java.lang.reflect.Method.invoke(Method.java:521)
07-04 17:05:32.140: ERROR/AndroidRuntime(280):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-04 17:05:32.140: ERROR/AndroidRuntime(280):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-04 17:05:32.140: ERROR/AndroidRuntime(280):     at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<p>Line 577 is <code>String selectedname2= selectedname.substring(1, 3);</code></p>

<p>This line would be   <code>String selectedname2= selectedname.substring(1, sn);</code>, i was just trying to figure out what is wrong.</p>

<p><strong>Edit2: Whole code:</strong></p>

<pre><code>public class Alarm extends Activity {

    public ArrayList&lt;String&gt; myArr = new ArrayList&lt;String&gt;();   
    String contactName;
    int spaceIndex, spaceLastIndex, spaceIndex2;
    String selectedname;
    String selectedname2;
    int sn;
    int selnamelength;
    String[] words;
    String surname;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm);

contactName = null;
Context context = getApplicationContext();
Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

while (cursor.moveToNext())
{
    contactName  = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
    spaceIndex =  contactName.indexOf(' ');
    spaceLastIndex =  contactName.lastIndexOf(' ');
    if (spaceIndex &gt; 0)     {myArr.add(contactName);}
    else if (spaceLastIndex &gt; 0)        {myArr.add(contactName);}
    else      {myArr.add(contactName);}
}
myArr.add("");
Collections.sort(myArr);

Spinner sp1 = (Spinner)findViewById(R.id.Spinner01);
final ArrayAdapter adapter3 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, myArr);
sp1.setAdapter(adapter3);
sp1.setOnItemSelectedListener(new MyOnItemSelectedListener());

}

public class MyOnItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView&lt;?&gt; parent,
        View view, int pos, long id) {
        selectedname = parent.getItemAtPosition(pos).toString();
        sn = selectedname.indexOf(' ');
        selnamelength = selectedname.length();
        selectedname2 = selectedname.substring(1, 3);  //force closes

        if (!selectedname.equals(""))
        {
            Toast.makeText(Alarm.this, "selectedname.length = " + selnamelength, Toast.LENGTH_LONG).show(); //This is working
        }

    }

    public void onNothingSelected(AdapterView parent) {

    }
}
}
</code></pre>

<p>By the way, split() function is working...</p>
