title: java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
body: <p>Hi in the below code I am getting array index out of bounds exception.Here friend array it's giving two values.</p>
  
  <p>For ex:
  friendinfo[0]=user1,friendinfo<a href="http://i.stack.imgur.com/bihp4.png" rel="nofollow">1</a>=user2 and with checkbox when i am selecting user1 I want to show friend.length to 2 and checked value should be 1.</p>
  
  <p>this is sample screen how to add the use3 and user1 when i am clicking the create button.
  <strong>GroupList.java</strong></p>
  
  <pre><code>    public class GroupList extends ListActivity 
  {
  
      boolean[] checkBoxState;
  
      private IAppManager imService = null;
  
      private FriendListAdapter friendAdapter;
  
      public String ownusername = new String();
  
      private class FriendListAdapter extends BaseAdapter 
      {   
          @SuppressWarnings("unused")
  
          class ViewHolder {
              TextView text;
              ImageView icon;
              CheckBox check1;
  
  
  
          }
          private LayoutInflater mInflater;
          private Bitmap mOnlineIcon;
          private Bitmap mOfflineIcon;        
  
          private FriendInfo[] friends = null;
  
  
  
  
          public FriendListAdapter(Context context) {
              super();            
  
              mInflater = LayoutInflater.from(context);
  
              mOnlineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.greenstar);
              mOfflineIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.redstar);
  
          }
  
          public void setFriendList(FriendInfo[] friends)
          {
              this.friends = friends;
  
          }
  
  
          public int getCount() {     
  
              return friends.length;
          }
  
  
          public FriendInfo getItem(int position) {           
  
              return friends[position];
          }
  
          public long getItemId(int position) {
  
              return 0;
          }
  
          public View getView(final int position, View convertView, ViewGroup parent) {
  
              final ViewHolder holder;
  
  
              if (convertView == null) 
              {
                  convertView = mInflater.inflate(R.layout.grouplist, null);
  
  
                  holder = new ViewHolder();
  
                  holder.text = (TextView) convertView.findViewById(R.id.text);
                  holder.icon = (ImageView) convertView.findViewById(R.id.icon);
                  holder.check1 = (CheckBox) convertView.findViewById(R.id.checkBox1);
  
                  convertView.setTag(holder);
  
              }           
  
              else {
  
                  holder = (ViewHolder) convertView.getTag();
              }
  
  
              holder.text.setText(friends[position].userName);
              holder.icon.setImageBitmap(friends[position].status == STATUS.ONLINE ? mOnlineIcon : mOfflineIcon);
              checkBoxState = new boolean[friends.length];
              holder.check1.setChecked(checkBoxState[position]);
              holder.check1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
  
                  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                      checkBoxState[position]=isChecked;
  
                      String check=friends[position].userName;
  
  
                      Toast.makeText(getApplicationContext(),friends[position].userName+"checked", Toast.LENGTH_LONG).show();
                  }
              });
  
  
              return convertView;
          }
  
      }
  
  
      public class MessageReceiver extends  BroadcastReceiver  {
  
          @Override
          public void onReceive(Context context, Intent intent) {
  
              Log.i("Broadcast receiver ", "received a message");
              Bundle extra = intent.getExtras();
              if (extra != null)
              {
                  String action = intent.getAction();
                  if (action.equals(IMService.FRIEND_LIST_UPDATED))
                  {
  
                      GroupList.this.updateData(FriendController.getFriendsInfo(), 
                                                  FriendController.getUnapprovedFriendsInfo());
  
                  }
              }
          }
  
      };
      public MessageReceiver messageReceiver = new MessageReceiver();
  
      private ServiceConnection mConnection = new ServiceConnection() {
          public void onServiceConnected(ComponentName className, IBinder service) {          
              imService = ((IMService.IMBinder)service).getService();      
  
              FriendInfo[] friends = FriendController.getFriendsInfo(); 
              if (friends != null) {              
                  GroupList.this.updateData(friends, null); 
              }    
              String groupname = getIntent().getStringExtra("nick");
              setTitle(groupname);
              ownusername = imService.getUsername();
          }
          public void onServiceDisconnected(ComponentName className) {          
              imService = null;
              Toast.makeText(GroupList.this, R.string.local_service_stopped,
                      Toast.LENGTH_SHORT).show();
          }
      };
  
  
  
      @SuppressLint("NewApi")
      @TargetApi(Build.VERSION_CODES.GINGERBREAD)
      protected void onCreate(Bundle savedInstanceState) 
      {       
          super.onCreate(savedInstanceState);
          if (android.os.Build.VERSION.SDK_INT &gt; 9) {
              StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
              .permitAll().build();
              StrictMode.setThreadPolicy(policy);
          }
          setContentView(R.layout.group_list_screen);
          friendAdapter = new FriendListAdapter(this);
          Button create=(Button)findViewById(R.id.create);
          create.setOnClickListener(new OnClickListener() {
  
              @SuppressWarnings("unused")
              @Override
              public void onClick(View v) {
                  String groupname = getIntent().getStringExtra("nick");
  
  
                          try {
  
                              FriendInfo[] friend=FriendController.getFriendsInfo();
                              //checkBoxState = new CheckBox[friend.length];
                              /*try {
                                  for(int i=0;i &lt;=friend.length ;i++){
  
                                      if(checkBoxState[i].isChecked()){
                                          check[i]="1";
                                      }
  
  
  
                              } 
                              }catch (Exception e) {
                                  e.printStackTrace();
                              }*/
  
  
                              String result1 = imService.CreateGroup(groupname,imService.getUsername(),friend);
                          } catch (UnsupportedEncodingException e) {
  
                              e.printStackTrace();
                          }
  
  
                  Toast.makeText(getApplicationContext(), "Group Created Sucessfully",Toast.LENGTH_LONG).show();
  
              }
          });
  
  
  
  
  
  
  
      }
  
      public void updateData(FriendInfo[] friends, FriendInfo[] unApprovedFriends)
      {
          if (friends != null) {
              friendAdapter.setFriendList(friends);   
              setListAdapter(friendAdapter);              
          }               
  
          if (unApprovedFriends != null) 
          {
              NotificationManager NM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  
              if (unApprovedFriends.length &gt; 0)
              {                   
                  String tmp = new String();
                  for (int j = 0; j &lt; unApprovedFriends.length; j++) {
                      tmp = tmp.concat(unApprovedFriends[j].userName).concat(",");            
                  }
                  NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                  .setSmallIcon(R.drawable.stat_sample)
                  .setContentTitle(getText(R.string.new_friend_request_exist));
                  /*Notification notification = new Notification(R.drawable.stat_sample, 
                          getText(R.string.new_friend_request_exist),
                          System.currentTimeMillis());*/
  
                  Intent i = new Intent(this, UnApprovedFriendList.class);
                  i.putExtra(FriendInfo.FRIEND_LIST, tmp);                
  
                  PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                          i, 0);
  
                  mBuilder.setContentText("You have new friend request(s)");
  
  
                  mBuilder.setContentIntent(contentIntent);
  
  
                  NM.notify(R.string.new_friend_request_exist, mBuilder.build());         
              }
              else
              {
  
                  NM.cancel(R.string.new_friend_request_exist);           
              }
          }
  
      }
  
  
  
  
  
      @Override
      protected void onPause() 
      {
          unregisterReceiver(messageReceiver);        
          unbindService(mConnection);
          super.onPause();
      }
  
      @Override
      protected void onResume() 
      {
  
          super.onResume();
          bindService(new Intent(GroupList.this, IMService.class), mConnection , Context.BIND_AUTO_CREATE);
  
          IntentFilter i = new IntentFilter();
  
          i.addAction(IMService.FRIEND_LIST_UPDATED);
  
          registerReceiver(messageReceiver, i);           
  
  
      }
  
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  
          super.onActivityResult(requestCode, resultCode, data);
  
  
  
  
      }
  </code></pre>