Solved

java.lang.IllegalThreadStateException in MIDlet

Posted on 2007-03-28
7
859 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 30

Expert Comment

by:Mayank S
ID: 18808962
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Systems talking to each other 5 123
ArrayIndexOutOfBoundException 9 66
maven project error 5 47
Oracle SQL syntax check  without executing 6 29
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now