title: Android: thread exiting with uncaught exception
body: <p>I have got problem and I can't find a solution for it. When I receive data over Bluetooth connection, my program suddenly crashes. I am receiving data in separate thread where is each received byte saved in queue.
   I also have one TimerTask, which calls method for read data from queue every 1 ms, and refresh TextBox in UI. My program works for a while, but then suddenly crashes. Please help. Here are the main code snippets.</p>
  
  <pre><code>public class MainActivity extends Activity
  {       
      TextView myLabel1;
      volatile boolean stopWorker = false;
  
  
      public Queue&lt;Byte&gt; receiverQueue = new LinkedList&lt;Byte&gt;();
      protected byte[] receiverArray;
      private boolean newDataRead = false;
  
      //timer
      TimerTask mTimerTask;
      Timer timer = new Timer();  
      final Handler timerHandler = new Handler();
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
  
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  myLabel1 = (TextView)findViewById(R.id.textView1);
  //start with timer ticking after 1 sec, and give timer tick every 1ms
  
  onTimerTick();
  timer.schedule(mTimerTask, 1000, 1); 
  }
  
  
  //method for reading data from receiverQueue
  protected void readDataFromReceiverBuffer() {
      //if receiverQueue is not empty, read data
      if(receiverQueue.isEmpty() == false) {
          //declaration of byte array receiverField size of receiverQueue
          receiverArray = new byte[receiverQueue.size()];
          //read bytes from queue until is empty
              for (int i = 0; receiverQueue.isEmpty() == false; i++) {
                  //retrieves and removes the head of this queue, or returns null 
              receiverArray[i] = receiverQueue.poll();
              }
          //variable newData is set to true, when we read new data from queue
          newDataRead = true;
      }
  }       
  
  
  Thread dataReceiveThread = new Thread(new Runnable() {
      //received byte
      int receivedData;
      public void run() {
          while(!Thread.currentThread().isInterrupted() &amp;&amp; !stopWorker) {
  
              try { 
                  //read one byte from InputStream
                  receivedData = mmInStream.read();
                  //inserts received byte into this queue if it is possible 
                  if(receivedData != -1)
                      receiverQueue.offer((byte)receivedData);
              }
              catch (IOException ex) {
                  stopWorker = true;
              }           
              //Log.i(String.valueOf(receivedData), "valueOfReceivedData");
          }           
      }       
  }); 
  
  public void onTimerTick() {
  mTimerTask = new TimerTask() {
      String temp;
      //this method is called 
          public void run() {
              //call method for read data from queue
              readDataFromReceiverBuffer();
              timerHandler.post(new Runnable() {
                  public void run() {
                      //when new data has arrived, we update UI 
                  if(newDataRead == true) {
                      //convert byte array to string
                      temp = new String(receiverArray, 0, receiverArray.length);
                       myLabel1.setText(temp);
                       newDataRead = false;
                  }                               
                      }
                  }); 
        }};    
  }    
  </code></pre>
  
  <p>LogCat:</p>
  
  <pre><code>W/dalvikvm(12358): threadid=11: thread exiting with uncaught exception (group=0x40ab1210)
  E/AndroidRuntime(12358): FATAL EXCEPTION: Timer-0
  E/AndroidRuntime(12358): java.lang.ArrayIndexOutOfBoundsException: length=21; index=21
  E/AndroidRuntime(12358):    at com.example.colibri2bb.MainActivity.readDataFromReceiverBuffer(MainActivity.java:195)
  E/AndroidRuntime(12358):    at com.example.colibri2bb.MainActivity.readDataFromReceiverBuffer(MainActivity.java:195)
  E/AndroidRuntime(12358):    at com.example.colibri2bb.MainActivity$3.run(MainActivity.java:230)
  E/AndroidRuntime(12358):    at java.util.Timer$TimerImpl.run(Timer.java:284)
  </code></pre>
  
  <p>LogCat:</p>
  
  <pre><code>W/dalvikvm(17344): threadid=12: thread exiting with uncaught exception (group=0x40ab1210)
  E/AndroidRuntime(17344): FATAL EXCEPTION: Timer-1
  E/AndroidRuntime(17344): java.util.NoSuchElementException
  E/AndroidRuntime(17344): atjava.util.LinkedList.removeFirstImpl(LinkedList.java:689)
  E/AndroidRuntime(17344): at java.util.LinkedList.removeFirst(LinkedList.java:676)
  E/AndroidRuntime(17344): at java.util.LinkedList.poll(LinkedList.java:895)
  E/AndroidRuntime(17344):atcom.example.colibri2bb.MainActivity.readDataFromReceiverBuffer(MainActivity.java:196)
  E/AndroidRuntime(17344): atcom.example.colibri2bb.MainActivity$3.run(MainActivity.java:231)
  E/AndroidRuntime(17344): at java.util.Timer$TimerImpl.run(Timer.java:284)
  </code></pre>