title: Non trivial (seemingly) StringIndexOutOfBoundsException
body: <p>Our crash logging system shows a crash and I can't understand how does it happen. The user inputs are unknown. I've added logging, but results will be visible only after the next release, ~2 weeks.</p>
  
  <p>How can the code below throw the next exception: </p>
  
  <pre><code>Crashes with java.lang.StringIndexOutOfBoundsException length=0; index=1
  </code></pre>
  
  <p>Not because of a concurrency issue? In other words, what might be the situation in which it crashes and how to avoid it (without wrapping it into <code>try-catch</code>)? What could be the case if it's a concurrency issue (I don't see one but maybe I'm wrong)?</p>
  
  <pre><code>private String contactName; // has getter and setter
  
  public String getFirstSymbol() {
    String firstSymbol = "";
    String trimmed = contactName.trim();
    if (!TextUtils.isEmpty(trimmed)) { // TextUtils.isEmpty is Android method: return str == null || str.length() == 0;
      String[] parts = trimmed.toUpperCase().split("\\s+");
      firstSymbol = parts[0].substring(0, 1); // crashes here:
    }
    return firstSymbol;
   }
  </code></pre>
  
  <p>There are ~100 crashes on 7 devices in the last week.</p>
  
  <p>Here is the stack trace    </p>
  
  <pre><code>  Fatal Exception: java.lang.StringIndexOutOfBoundsException: length=0; index=1
    at java.lang.String.substring(String.java:1971)
    at my.app.model.Contact.getFirstSymbol(Contact.java:256)
    at my.app.ui.AvatarView.bind(AvatarView.java:73)
    at my.app.viewholder.CallLogViewHolder.bind(CallLogViewHolder.java:50)
    at my.app.viewholder.CallLogViewHolder.bind(CallLogViewHolder.java:23)
    at my.app.ContactListAdapter.onBindViewHolder(ContactListAdapter.java:68)
    at my.app.ContactListAdapter.onBindViewHolder(ContactListAdapter.java:25)
    at androidx.recyclerview.widget.RecyclerView$Adapter.onCreateViewHolder(RecyclerView.java:6781)
    at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
    at androidx.recyclerview.widget.RecyclerView$Adapter.getItemCount(RecyclerView.java:6781)
    at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
    at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
    at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
    at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
    at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
    at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4194)
    at android.view.View.layout(View.java:20964)
    at android.view.ViewGroup.layout(ViewGroup.java:6440)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:20964)
    at android.view.ViewGroup.layout(ViewGroup.java:6440)
    at androidx.viewpager.widget.ViewPager.onLayout(ViewPager.java:1775)
    at android.view.View.layout(View.java:20964)
    at android.view.ViewGroup.layout(ViewGroup.java:6440)
    at com.google.android.material.appbar.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:142)
    at com.google.android.material.appbar.HeaderScrollingViewBehavior.findFirstDependency(HeaderScrollingViewBehavior.java:142)
    at com.google.android.material.appbar.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:41)
    at com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1556)
    at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:888)
    at android.view.View.layout(View.java:20964)
    at android.view.ViewGroup.layout(ViewGroup.java:6440)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:20964)
    at android.view.ViewGroup.layout(ViewGroup.java:6440)
    at com.google.android.material.appbar.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:142)
    at com.google.android.material.appbar.HeaderScrollingViewBehavior.findFirstDependency(HeaderScrollingViewBehavior.java:142)
    at com.google.android.material.appbar.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:41)
    at com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1556)
    at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:888)
    at android.view.View.layout(View.java:20964)
    at android.view.ViewGroup.layout(ViewGroup.java:6440)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:20964)
    at android.view.ViewGroup.layout(ViewGroup.java:6440)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
    at android.view.View.layout(View.java:20964)
    at android.view.ViewGroup.layout(ViewGroup.java:6440)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:20964)
    at android.view.ViewGroup.layout(ViewGroup.java:6440)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
    at android.view.View.layout(View.java:20964)
    at android.view.ViewGroup.layout(ViewGroup.java:6440)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at com.android.internal.policy.DecorView.onLayout(DecorView.java:955)
    at android.view.View.layout(View.java:20964)
    at android.view.ViewGroup.layout(ViewGroup.java:6440)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3092)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2779)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1863)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8072)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
    at android.view.Choreographer.doCallbacks(Choreographer.java:723)
    at android.view.Choreographer.doFrame(Choreographer.java:658)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:7000)
    at java.lang.reflect.Method.invoke(Method.java)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
  </code></pre>