Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

java.lang.IllegalThreadStateException in MIDlet

Posted on 2007-03-28
7
Medium Priority
?
900 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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

618 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