Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 20
Registered: ‎09-13-2011
My Device: 9780
My Carrier: Vodafone
Accepted Solution

Illegal State Exception when running a code at startup

[ Edited ]

Here is my main method:

 

	public static void main(String[] args) 
	{
		if (args.length == 1 && args[0].equals("startup")) 
		{
			Criteria locationCriteria = new Criteria();
			locationCriteria.setCostAllowed(false);
			LocationProvider mlocationProvider;
			Location mLocation = null;
			try 
			{
				mlocationProvider = LocationProvider
						.getInstance(locationCriteria);
				mLocation = mlocationProvider.getLocation(-1);
			} 
			catch (LocationException e) {
			} 
			catch (InterruptedException e) {
			}
			QualifiedCoordinates mQC = mLocation.getQualifiedCoordinates();
		} 
		else 
		{
			MyApp theApp = new MyApp();
			theApp.enterEventDispatcher();
		}
	}

 

 

 

 

 

 

The method 

 mlocationProvider = LocationProvider.getInstance(locationCriteria); 

throws illegal state exception 

When I check the debug info I found this exception is thrown at the line when it calls Application.getApplication();

 

When I move this code to be executed in a normal screen it works fine.!

Any help ?

Highlighted
Developer
Posts: 1,627
Registered: ‎07-14-2008
My Device: Z10
My Carrier: Fido

Re: Illegal State Exception when running a code at startup

some of the functions need to be called from a GUI thread.

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Illegal State Exception when running a code at startup

[ Edited ]

There are potentially a number of issues here:

 

1) Until your Application is actually running, you should not really do any processing.  Your Application does not start running until you

'enterEventDispatcher()'

 

So in main, all you should really do is instantiate your Application.  Your Application's constructor should not do anything complicated either, since it runs as part of main().

 

You can do some activities, for example to add listeners, in main() code which is, in some respects, unfortunate because it lulls people into thinking they can do anything.  ,

 

2) Getting the location as you are doing is a Blocking call.  So you need to do it on a Background Thread.  You c an get away with this on the Simulator because the simulated GPS comes back immediately with a location.  So it does not actually block.  But on a real device, code such as you have may cause your application to break.

 

3) You seem to be trying to do something in start-up.  You need to be aware, that start-up up is called as part of the device startup and before the the device is fully active.  In fact I believe on a real device this code will fail because the device is not ready to provide a location at start up.

 

You will find find the following KB article enlightening and useful for (1) and (3).

http://supportforums.blackberry.com/t5/Java-Development/Write-safe-initialization-code/ta-p/444795

 

I suspect you will want to start and Application to obtain a location at start up, in which case you might find this useful:

http://supportforums.blackberry.com/t5/Java-Development/Create-a-Background-Application/ta-p/445226

 

Contributor
Posts: 20
Registered: ‎09-13-2011
My Device: 9780
My Carrier: Vodafone

Re: Illegal State Exception when running a code at startup

[ Edited ]

Peter, you are absolutely right (Y)

 

Thanks