Solved

java.lang.IllegalThreadStateException in MIDlet

Posted on 2007-03-28
7
848 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:mayankeagle
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:mayankeagle
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

708 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

17 Experts available now in Live!

Get 1:1 Help Now