Buggy code:
public class TabletActivity extends Activity implements WifiP2pManager . ConnectionInfoListener , Handler . Callback { public static final String TAG = "tableActivity" ; public static final String TABLE_SERVICE = "_coffee_server_service" ; public static final String DEVICE_SERVICE = "_coffee_client_service" ; public static final String SERVICE_REG_TYPE = "_presence._tcp" ; public static final int SERVER_PORT = 4545 ; public static final int MESSAGE_READ = 1024 + 1 ; public static final int MY_HANDLE = 1024 + 2 ; private WifiP2pManager manager ; private final IntentFilter intentFilter = new IntentFilter ( ) ; private Channel channel ; private BroadcastReceiver receiver = null ; private WifiP2pDnsSdServiceRequest serviceRequest ; private Handler handler = new Handler ( this ) ; private MsgManager msgManager = null ; private UsersListAdapter adapter ; private Timer timer = null ; private int minutes = 2 ; private int nextCM = 0 ; private Random rand = null ; @ Override protected void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ) ; setContentView ( activity_tablet ) ; intentFilter . addAction ( WIFI_P2P_STATE_CHANGED_ACTION ) ; intentFilter . addAction ( WIFI_P2P_PEERS_CHANGED_ACTION ) ; intentFilter . addAction ( WIFI_P2P_CONNECTION_CHANGED_ACTION ) ; intentFilter . addAction ( WIFI_P2P_THIS_DEVICE_CHANGED_ACTION ) ; manager = ( ( WifiP2pManager ) ( getSystemService ( WIFI_P2P_SERVICE ) ) ) ; channel = manager . initialize ( this , getMainLooper ( ) , null ) ; Button playBtn = ( ( Button ) ( findViewById ( playBtn ) ) ) ; msgManager = MsgManager . getInstance ( ) ; adapter = new UsersListAdapter ( this , layout . simple_list_item_2 , id . text1 , new ArrayList < android . net . wifi . p2p . WifiP2pDevice > ( ) ) ; ListView userList = ( ( ListView ) ( findViewById ( regList ) ) ) ; userList . setAdapter ( adapter ) ; playBtn . setOnClickListener ( new Button . OnClickListener ( ) { @ Override public void onClick ( View v ) { manager . discoverServices ( channel , new WifiP2pManager . ActionListener ( ) { @ Override public void onSuccess ( ) { Log . d ( TabletActivity . TAG , "Service<seq2seq4repair_space>discovery<seq2seq4repair_space>initiated." ) ; } @ Override public void onFailure ( int arg0 ) { Log . d ( TabletActivity . TAG , "Service<seq2seq4repair_space>discovery<seq2seq4repair_space>failed." ) ; } } ) ; timer = new Timer ( ) ; rand = new Random ( ) ; <START_BUG> nextCM = rand . nextInt ( adapter . getSize ( ) ) ; <END_BUG> while ( true ) { timer . schedule ( new TimerTask ( ) { public void run ( ) { ( nextCM ) ++ ; if ( ( ( nextCM ) + 1 ) == ( adapter . getSize ( ) ) ) { nextCM = 0 ; } MsgManager . getInstance ( ) . write ( ( "ContentMaster" + ( adapter . getItemName ( nextCM ) ) ) . getBytes ( ) ) ; } } , 0 , ( ( 60 * 1000 ) * ( minutes ) ) ) ; } } } ) ; registerServerService ( ) ; } @ Override protected void onStop ( ) { } @ Override public void onResume ( ) { } @ Override public void onPause ( ) { } @ Override public boolean onCreateOptionsMenu ( Menu menu ) { } @ Override public boolean onOptionsItemSelected ( MenuItem item ) { } public void updateUserList ( WifiP2pGroup devices ) { } private void registerServerService ( ) { } private void startDiscovery ( ) { } @ Override public void onConnectionInfoAvailable ( WifiP2pInfo info ) { } @ Override public boolean handleMessage ( Message msg ) { } }

Fix:
nextCM = rand . nextInt ( ( ( adapter . getSize ( ) ) - 1 ) ) ;