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
LMcRae
Posts: 163
Registered: ‎04-16-2009
My Device: Not Specified
Accepted Solution

What are people using for assert?

Sorry if this is more of a Java question but my Java Fu isn't the strongest.  I want to add something equivalent to an assert in C/C++.  This is what I rolled but I can get flooded with a ton of these dialogs.  I think it happens when I have various threads all calling RIP. Is there a better way to do it?  I would want the app to halt until I have read the message.

 

 

public static void RIP( final String msg )
{
	UiApplication.getApplication().invokeLater( new Runnable()
	{
		public void run()
		{
			Dialog.alert( msg );
			System.exit( 0 );
		}
	});
}

 

 

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: What are people using for assert?

create a dialog beforehand and check isDisplayed before you display it to avoid displaying it twice.

----------------------------------------------------------
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
Developer
LMcRae
Posts: 163
Registered: ‎04-16-2009
My Device: Not Specified

Re: What are people using for assert?

Yup.  Nice simple solution.  It won't stop the app though will it?  I would have thought there would be something in the OS for an assert.

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: What are people using for assert?

system.exit kills the app. and dialog.alert is blocking, that means that system.exit is only called after you closed the button.

 

i don't understand what you mean with assert, i know it only from testing (asserttrue etc)

----------------------------------------------------------
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
Developer
LMcRae
Posts: 163
Registered: ‎04-16-2009
My Device: Not Specified

Re: What are people using for assert?

In C/C++, if you call assert with a condition that isn't true, the program will exit after it has given you a message somehow.  I say somehow since it could be through a console, a popup dialog etc.  I am not sure my RIP along with your suggestion will halt my app and display the dialog until I dismiss it.  Can another one of my threads come along and do something, such as push/pop a screen, that will cause the Alert to be hidden or dismissed? 

Developer
DAquilina
Posts: 587
Registered: ‎01-19-2010
My Device: BOLD 9700 OS5.0.0.x on Rogers Canada
My Carrier: Rogers

Re: What are people using for assert?

[ Edited ]

I miss the preprocessor...

 

I'm fairly new to Blackberry programming and/or multi-Threadding myself, by I'm thinking if you call a delay when the dialog is output it will basically be dead in the water. You can then have an "accept" button that calls Thread.currentThread ().interrupt when it's clicked. I don't know if this will circumvent the delay, but it's worth a try. As for making this method more assert-like, you can enclose every instance in an if statement that checks for some boolean that is only true when main.args [n] is "debug".

 

Hope that helps,

~Dom

 

EDIT: I should mention that interrupt is likely to throw an exception, which you should handle according to your needs.

----------------------------------------------------------------------------
chown -R us ./base
~J!NX
Developer
LMcRae
Posts: 163
Registered: ‎04-16-2009
My Device: Not Specified

Re: What are people using for assert?

Tell me about it.  There is some support for the preprocessor to though.  Search this forum for more detials.  I find that NetBeans has the best support since it will comment out the code that has been disabled by the preprocessor.  This makes the code play nice with all the IDE features.  Also you can make different targets that allow for stuff like debug, beta and release. 

 

Here is what I ended up going with.

 

 

 

public class Assert
{
	private static boolean	ShowingRIP = false;

	public static final void Assert( final boolean bTruth, final String msg )
	{
		if ( bTruth == false )
			RIP( msg );
	}
	public static final void Assert( final Object obj, final String msg )
	{
		if ( obj == null )
			RIP( msg );
	}
	public static final void RIP( final String msg )
	{
		if ( ShowingRIP == false )
		{
			ShowingRIP = true;
			UiApplication.getApplication().invokeAndWait( new Runnable()
			{
				public void run()
				{
					Dialog.alert( msg );
					System.exit( 0 );
				}
			});
		}
	}
}

 

 

 

Developer
DAquilina
Posts: 587
Registered: ‎01-19-2010
My Device: BOLD 9700 OS5.0.0.x on Rogers Canada
My Carrier: Rogers

Re: What are people using for assert?

That looks a lot safer than my idea lol. As I said, I'm new to all this. Plus I haven't been working with UI, so I don't know that API too well.

 

Thanks, and good luck.

~Dom

----------------------------------------------------------------------------
chown -R us ./base
~J!NX
Developer
schepurny
Posts: 168
Registered: ‎11-22-2009
My Device: 9000 Bold, 8300 Curve

Re: What are people using for assert?

[ Edited ]

Using Assert is good for C++, but for Java there is Exception handling built in. If you handle exceptions properly and use proper test cases, then Assert is not as necessary (my opinion).

Developer
DAquilina
Posts: 587
Registered: ‎01-19-2010
My Device: BOLD 9700 OS5.0.0.x on Rogers Canada
My Carrier: Rogers

Re: What are people using for assert?

Assert is used to debug. You put assert calls wherever you want a debug message printed, and if "debug" is defined by the preprocessor then it outputs the desired message. If "debug" is not defined, Assert does nothing.

----------------------------------------------------------------------------
chown -R us ./base
~J!NX