title: BlackBerry - Exception when sending SMS
body: <p>The code below should send a text message to a mobile number. It currently fails to work properly.</p>
  <p>When the program attempts a message, the following error is reported:</p>
  <blockquote>
  <p>Blocking operation not permitted on event dispatch thread</p>
  </blockquote>
  <p>I created a separate thread to execute the SMS code, but I am still observing the same exception.</p>
  <p>What am I doing wrong?</p>
  <pre><code>class DummyFirst extends MainScreen {
      private Bitmap background;
      private VerticalFieldManager _container;
      private VerticalFieldManager mainVerticalManager;
      private HorizontalFieldManager horizontalFldManager;
      private BackGroundThread _thread;
      CustomControl buttonControl1;
      
      public DummyFirst() {
          super();
          LabelField appTitle = new LabelField(&quot;Dummy App&quot;);
          setTitle(appTitle);
          background = Bitmap.getBitmapResource(&quot;HomeBack.png&quot;);
          _container = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL
                  | Manager.NO_VERTICAL_SCROLLBAR) {
              protected void paint(Graphics g) {
                  // Instead of these next two lines, draw your bitmap
                  int y = DummyFirst.this.getMainManager()
                          .getVerticalScroll();
                  g.clear();
                  g.drawBitmap(0, 0, background.getWidth(), background
                          .getHeight(), background, 0, 0);
                  super.paint(g);
              }
              protected void sublayout(int maxWidth, int maxHeight) {
                  int width = background.getWidth();
                  int height = background.getHeight();
                  super.sublayout(width, height);
                  setExtent(width, height);
              }
          };
          mainVerticalManager = new VerticalFieldManager(
                  Manager.NO_VERTICAL_SCROLL | 
                  Manager.NO_VERTICAL_SCROLLBAR) {
              protected void sublayout(int maxWidth, int maxHeight) {
                  int width = background.getWidth();
                  int height = background.getHeight();
                  super.sublayout(width, height);
                  setExtent(width, height);
              }
          };
          HorizontalFieldManager horizontalFldManager = 
                      new HorizontalFieldManager(Manager.USE_ALL_WIDTH);
          buttonControl1 = new CustomControl(&quot;Send&quot;, ButtonField.CONSUME_CLICK,
                  83, 15);
          horizontalFldManager.add(buttonControl1);
          this.setStatus(horizontalFldManager);
          FieldListener listner = new FieldListener();
          buttonControl1.setChangeListener(listner);
          _container.add(mainVerticalManager);
          this.add(_container);
      }
  
      class FieldListener implements FieldChangeListener {
          public void fieldChanged(Field f, int context) {
              if (f == buttonControl1) {
                  _thread = new BackGroundThread();
                  _thread.start();
              }
          }
      }
  
      private class BackGroundThread extends Thread {
          public BackGroundThread() {
              /*** initialize parameters in constructor *****/
          }
          public void run() {
              // UiApplication.getUiApplication().invokeLater(new Runnable()
              UiApplication.getUiApplication().invokeLater(new Runnable() {
                  public void run() {
                      try {
                          MessageConnection msgConn = 
                                      (MessageConnection) Connector
                                  .open(&quot;sms://:0&quot;);
                          Message msg = msgConn
                              .newMessage(
                                  MessageConnection.TEXT_MESSAGE);
                          TextMessage txtMsg = (TextMessage) msg;
                          String msgAdr = &quot;sms://+919861348735&quot;;
                          txtMsg.setAddress(msgAdr);
                          txtMsg.setPayloadText(&quot;Test Message&quot;);
                                  // here exception is thrown
                          msgConn.send(txtMsg);
                          System.out.println(&quot;Sending&quot; + 
                              &quot; SMS success !!!&quot;);
                      } catch (Exception e) {
                          System.out.println(e.getMessage());
                          e.printStackTrace();
                      }
                  } // run
              });
          }
      }
  
      public boolean onClose() {
          System.out.println(&quot;close event called, request to be&quot; + 
              &quot; in the backgroud....&quot;);
          UiApplication.getUiApplication().requestBackground();
          return true;
      }
  }
  </code></pre>