If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

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
Developer
nikita18
Posts: 144
Registered: ‎08-18-2012
My Device: Blackberry Curve 8520
My Carrier: Vodafone
Accepted Solution

Exception: No application instance

My code below:

 

try{
    	Application.getApplication().invokeLater(new Thread(){    	
    		public void run(){     			
    			if(!checkForAppStop()){
    	    		FetchImg thrd=new FetchImg();				
    				thrd.start();				    										    	
    		    	new UploadThread().start();	//start thread		       
    	    	}
    	    	else 
    	    		System.out.println("Application stopped");    
    		}
    	});
    	} catch(Exception e){
    		 System.out.println("====Exception: "+e.getMessage());
    	}

 

The line highlighted above is giving me the exception " No application instance". Can anyone guide me a solution for this? Thanks a lot.

Please use plain text.
Developer
simon_hain
Posts: 15,908
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Exception: No application instance

where do you use this code? maybe in the static main method?
in any case, the application object is not there at this time, you have to move the code some place else.

as a sidenote: don't use invokelater with a thread, it needs only a runnable.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
nikita18
Posts: 144
Registered: ‎08-18-2012
My Device: Blackberry Curve 8520
My Carrier: Vodafone

Re: Exception: No application instance

 Yes I've placed this code in main method. I edited my code to:

 

Application.getApplication().invokeLater(new Runnable(){

 

But its still giving me the same exception. What should I do now? 

Please use plain text.
Developer
simon_hain
Posts: 15,908
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Exception: No application instance

the main method is executed before there is an application instance, as you usually create one at its end and call enterEventDispatcher (which does not return).
place your code in the constructor, for example.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
nikita18
Posts: 144
Registered: ‎08-18-2012
My Device: Blackberry Curve 8520
My Carrier: Vodafone

Re: Exception: No application instance

[ Edited ]

I placed my code in the costructor and it solved my problem. Thanks so much Simon_hain. But I couldn't understand the reason for this. Could you please explain me a bit? Thanks again.

Please use plain text.
Developer
simon_hain
Posts: 15,908
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Exception: No application instance

the constructor for the application creates the application instance. before that there is none.
without an application instance you cannot use invokelater.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Exception: No application instance

Simon's explanation is exactly correct.  I don't think you can add much to it.  I will try to repeat it using different words and perhaps that will help.

 

When main is executed, there is NO application, there is just a Thread.  All main methods create an Application and then use enterEventDisplatcher to get the events running. 

 

When you code Application.getApplication(), you are trying to find the running application, this code goes to the OS and looks for the Application.  But in the middle of the Application constructor, you don't have one that the OS knows about.  Hence your problem.

 

Instead of using "Application.getApplication()", in your Application constructor you could actually code "this".   

 

HTH. 

Please use plain text.
Developer
nikita18
Posts: 144
Registered: ‎08-18-2012
My Device: Blackberry Curve 8520
My Carrier: Vodafone

Re: Exception: No application instance

Thank you simon_hain and peter_strange. That was a wonderful explaination from both of you. I'm glad I'm a part of this forum. Thanks again.

Please use plain text.