Solved

java.lang.IllegalThreadStateException in MIDlet

Posted on 2007-03-28
7
891 Views
Last Modified: 2013-12-29
Hi experts,

I tried to invoke web service in my Midlet in the commandAction, but it gives me the following Warning:

To avoid potential deadlock, operations that may block, such as
 networking, should be performed in a different thread than the
 commandAction() handler.

and didn't perfome web services calling

Then I have implemented Runnable, override run() and run my thread and it gives me the Exception:

java.lang.IllegalThreadStateException

this is my code:

/*
 * DBClient.java
 *
 * Created on March 21, 2007, 2:50 PM
 */

import java.rmi.RemoteException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import login.*;

public class DBClient extends MIDlet implements CommandListener, Runnable {
   
   
    Form mainForm = new Form ("Welcome");
    TextField username = new TextField ("Uaername", "", 10, TextField.ANY);
    TextField password = new TextField ("Password", "", 10, TextField.PASSWORD);
    StringItem resultItem = new StringItem ("", "");
    Command getCommand = new Command ("Login", Command.SCREEN, 1);
    Thread myThread = new Thread();    
    boolean isThread = true;
   
    public DBClient(){
   
      mainForm.append (username);
      mainForm.append (password);
      mainForm.append (resultItem);
      mainForm.addCommand (getCommand);
      mainForm.setCommandListener (this);
       
   
    }//
   
   
    public void startApp() {
       
         Display.getDisplay(this).setCurrent(mainForm);
         
       
           
       
       
    }
   
    public void pauseApp() {
    }
   
    public void destroyApp(boolean unconditional) {
    }

   
           
   
public void commandAction (Command c, Displayable d) {
   
   
    if (c == getCommand ){
   
      try {
           myThread.start();
      }
      catch (Exception e) {
          e.printStackTrace ();
          resultItem.setLabel ("Error:");
          resultItem.setText (e.toString ());
      }
    }// end of if
    }


 public void run(){
 
 while(isThread){
     
     Action a = new Action();
     
     String myresult = a.invokeLogin(username.getString(),password.getString());
     resultItem.setText(myresult);
     try{
    myThread.wait(1000);
     
     }
     catch(Exception e){}// end of catch
    isThread = false;
     
   
 }// end of while
 }// end of run thread


}
0
Comment
Question by:Sh_Rashed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 18808950
>> resultItem.setText(myresult);

You cannot modify the UI from another thread - you should do it from the Event Dispatch Thread.
0
 
LVL 12

Expert Comment

by:keyurkarnik
ID: 18834846
What mayank has stated above is NOT the issue . You can change the resultItem from anywhere you want.

Your problem is this :

You have created a thread myThread = new Thread();
and then you are doing myThread.start()

whereas your run method is in your DBClient class.

The code will not work this way. You should do it either using a separate class that extends thread or use an inner class or use an anonymous impelmentation of run.

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Accepted Solution

by:
keyurkarnik earned 200 total points
ID: 18834849
Here is a solution with an anonymous implementation :



/*
 * DBClient.java
 *
 * Created on March 21, 2007, 2:50 PM
 */

import java.rmi.RemoteException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class DBClient extends MIDlet implements CommandListener
{


      Form mainForm = new Form("Welcome");
      TextField username = new TextField("Uaername", "", 10, TextField.ANY);
      TextField password = new TextField("Password", "", 10, TextField.PASSWORD);
      StringItem resultItem = new StringItem("", "");
      Command getCommand = new Command("Login", Command.SCREEN, 1);
      Thread myThread = new Thread();
      boolean isThread = true;

      public DBClient()
      {

            mainForm.append(username);
            mainForm.append(password);
            mainForm.append(resultItem);
            mainForm.addCommand(getCommand);
            mainForm.setCommandListener(this);


      }//


      public void startApp()
      {

            Display.getDisplay(this).setCurrent(mainForm);


      }

      public void pauseApp()
      {
      }

      public void destroyApp(boolean unconditional)
      {
      }


      public void commandAction(Command c, Displayable d)
      {


            if (c == getCommand)
            {

                  try
                  {
                        new Thread()
                        {

                              public void run()
                              {

                                    while (isThread)
                                    {

                                          Action a = new Action();

                                          String myresult = a.invokeLogin(username.getString(), password.getString());
                                          resultItem.setText(myresult);
                                          try
                                          {
                                                sleep(1000);

                                          }
                                          catch (Exception e)
                                          {
                                          }// end of catch
                                          isThread = false;


                                    }// end of while
                              }// end of run thread

                        }.start();
                  }
                  catch (Exception e)
                  {
                        e.printStackTrace();
                        resultItem.setLabel("Error:");
                        resultItem.setText(e.toString());
                  }
            }// end of if
      }

}
0
 
LVL 12

Expert Comment

by:keyurkarnik
ID: 18834850
The rest is same - just take a look at the run method -
I have removed it from your class and put it as an anonymous method in the commandAction method.
0
 

Author Comment

by:Sh_Rashed
ID: 18834897
Thank u so much it works
0
 
LVL 12

Expert Comment

by:keyurkarnik
ID: 18834902
See - the problem you had was that you were starting a different thread - which actually had no runnable by doing myThread = new Thread().
thats why the illegal state exception
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question