?
Solved

java.lang.IllegalThreadStateException in MIDlet

Posted on 2007-03-28
7
Medium Priority
?
909 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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 12

Accepted Solution

by:
keyurkarnik earned 800 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

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!

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

840 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