beanshell beginner- how do I check if a variable.toString contains no value in it?

Arvind picture Arvind · Jul 20, 2012 · Viewed 8.2k times · Source

In a beanshell program, I get a value in a variable. Now, if there was no value obtained for this variable, then I want to set this as "Missing"

In a very old version of Beanshell I used the command as shown below to do this--

 contact.toString() != ''

However I updated the Beanshell library and now I am getting an error message that says that I cannot put a second apostrophe after the first one (i.e. '' is erroneous).

How do I check correctly for the above condition in Beanshell?

Answer

GaryMcM picture GaryMcM · Jan 7, 2013

An apostrophe is used in the Java language to indicate a char. You cannot have a '' char value. However the earlier version of beanshell may have been doing some implicit conversion to an empty string that somehow is broken after upgrade.

Checking for a valid value using the toString() seems a bit wasteful to me, but if 'contact' be non-null, yet still not have a value in the toString() representation the you check the String value as follows:

if(contact != null) {
   contactStr = contact.toString();
   if(contactStr != null @and contactStr.length() > 0) {
     /// value is something other than null or ""
   }
}

and the toString() is the only way