title: How to resolve java.lang.IndexOutOfBoundsException in android?
body: <p>I have created an expandable list view,On clicking the child element another activity is started.But this happens only for the first three child elements in first parent group, when i click on the subsequent child elements the app crashes an the error </p>
  
  <blockquote>
    <p>java.lang.IndexOutOfBounds:Invalid index 3,size is 3.</p>
  </blockquote>
  
  <p>I don't know where I have gone wrong.</p>
  
  <p>This is <code>MainActivity.java</code> file</p>
  
  <pre><code>    package com.example.expandablelistview;
  
      import java.util.ArrayList;
      import java.util.HashMap;
      import java.util.List;
      import android.app.Activity;
      import android.content.Intent;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.ExpandableListView;
      import android.widget.ExpandableListView.OnChildClickListener;
  
  
      public class MainActivity extends Activity {
  
      ExpandableListAdapter listAdapter;
      ExpandableListView expListView;
      List&lt;String&gt; listDataHeader;
      HashMap&lt;String, List&lt;String&gt;&gt; listDataChild;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      // get the listview
      expListView = (ExpandableListView) findViewById(R.id.lvExp);
  
      // preparing list data
      prepareListData();
  
      listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
  
      // setting list adapter
      expListView.setAdapter(listAdapter);
  
      expListView.setOnChildClickListener(new OnChildClickListener() {
  
          @Override
          public boolean onChildClick(ExpandableListView parent, View view,
                  int groupPosition, int childPosition, long id) {
  
              // selected item
  
              if(listDataHeader.get(groupPosition)=="Catalog"){
               Intent i = new Intent(MainActivity.this, SingleListItem.class);
         // sending data to new activity
            startActivity(i);
              }
  
              if(listDataHeader.get(groupPosition)=="My Account"){
  
                      Intent i1 = new Intent(MainActivity.this,CheckHolds.class);
                      startActivity(i1);
              }
               if(listDataHeader.get(groupPosition)=="Library Info"){
  
                  Intent i2 = new Intent(MainActivity.this,LibraryHours.class);
                  startActivity(i2);
              }
  
           return false;
          }
  
         });
        }
         /*
         * Preparing the list data
         */
        private void prepareListData() {
        listDataHeader = new ArrayList&lt;String&gt;();
        listDataHeader.add("Catalog");
        listDataHeader.add("My Account");
        listDataHeader.add("Library Info");
  
         // Adding child data
         List&lt;String&gt; catalog = new ArrayList&lt;String&gt;();
         catalog.add("Automobile");
         catalog.add("Civil");
         catalog.add("Electronics and Communication");
         catalog.add("Electrical and Electronics");
         catalog.add("Information Science");
         catalog.add("Industrial Production");
         catalog.add("Mechanical");
         catalog.add("Basic Sciences");
  
         List&lt;String&gt; myaccount = new ArrayList&lt;String&gt;();
         myaccount.add("Check Holds");
         myaccount.add("Unreserve Books");
  
         List&lt;String&gt; libraryinfo = new ArrayList&lt;String&gt;();
         libraryinfo.add("Library Hours");
         libraryinfo.add("Contact Library");
  
         listDataChild.put(listDataHeader.get(0), catalog); // Header, Child data
         listDataChild.put(listDataHeader.get(1), myaccount);
         listDataChild.put(listDataHeader.get(2), libraryinfo);
         }
        }
  </code></pre>
  
  <p>And this is the ExpandableListAdapter.java file</p>
  
  <pre><code>    package com.example.expandablelistview;
  
      import java.util.HashMap;
      import java.util.List;
  
      import android.content.Context;
      import android.graphics.Typeface;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.BaseExpandableListAdapter;
      import android.widget.TextView;
  
      public class ExpandableListAdapter extends BaseExpandableListAdapter {
  
       private Context _context;
       private List&lt;String&gt; _listDataHeader; // header titles
      // child data in format of header title, child title
       private HashMap&lt;String, List&lt;String&gt;&gt; _listDataChild;
  
       public ExpandableListAdapter(Context context, List&lt;String&gt; listDataHeader,
          HashMap&lt;String, List&lt;String&gt;&gt; listChildData) {
      this._context = context;
      this._listDataHeader = listDataHeader;
      this._listDataChild = listChildData;
      }
  
     @Override
      public Object getChild(int groupPosition, int childPosititon) {
      return this._listDataChild.get(this._listDataHeader.get(groupPosition))
              .get(childPosititon);
      }
  
      @Override
      public long getChildId(int groupPosition, int childPosition) {
      return childPosition;
       }
  
      @Override
        public View getChildView(int groupPosition, final int childPosition,
          boolean isLastChild, View convertView, ViewGroup parent) {
  
      final String childText = (String) getChild(groupPosition, childPosition);
  
      if (convertView == null) {
          LayoutInflater infalInflater = (LayoutInflater) this._context
                  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          convertView = infalInflater.inflate(R.layout.list_item, null);
      }
  
      TextView txtListChild = (TextView) convertView
              .findViewById(R.id.lblListItem);
  
      txtListChild.setText(childText);
      return convertView;
     }
  
      @Override
       public int getChildrenCount(int groupPosition) {
      return this._listDataChild.get(this._listDataHeader.get(groupPosition))
              .size();
       }
  
       @Override
       public Object getGroup(int groupPosition) {
      return this._listDataHeader.get(groupPosition);
       }
  
       @Override
      public int getGroupCount() {
      return this._listDataHeader.size();
     }
  
     @Override
      public long getGroupId(int groupPosition) {
      return groupPosition;
      }
  
     @Override
      public View getGroupView(int groupPosition, boolean isExpanded,
          View convertView, ViewGroup parent) {
      String headerTitle = (String) getGroup(groupPosition);
      if (convertView == null) {
          LayoutInflater infalInflater = (LayoutInflater) this._context
                  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          convertView = infalInflater.inflate(R.layout.list_group, null);
      }
  
      TextView lblListHeader = (TextView) convertView
              .findViewById(R.id.lblListHeader);
      lblListHeader.setTypeface(null, Typeface.BOLD);
      lblListHeader.setText(headerTitle);
  
      return convertView;
      }
  
      @Override
       public boolean hasStableIds() {
        return false;
      }
  
      @Override
      public boolean isChildSelectable(int groupPosition, int childPosition) {
      return true;
       }
      }
  </code></pre>
  
  <p>Please suggest me what changes should I do to solve the error.</p>