3

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?

Arvind
  • 6,404
  • 20
  • 94
  • 143

2 Answers2

4

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

GaryMcM
  • 425
  • 3
  • 9
2

If you want to check whether contact is defined then you do:

if (contact == void)

http://www.beanshell.org/manual/specialvarsvalues.html

Jarekczek
  • 7,456
  • 3
  • 46
  • 66