September 27, 2012

How to check for null values

This entry is part of the Maximo Java Development series.

A very common doubt when extending MBOs is what value returns the Mbo.getString() method when the target field has a null value. The answer is: It returns an empty string "".
Thus to check if the DESCRIPTION of an Mbo is empty or null you can use two basic approaches.
getString("DESCRIPTION").equals("")
or
getString("DESCRIPTION").length()==0

If you need do distinguish between null values and empty string you can use the Mbo.getMboValueData() method.
Thus to check if the DESCRIPTION of an Mbo is null use this code snippet.
getMboValueData("DESCRIPTION").isNull()

If you are in an Mbo class you can perform the same check as follows:
isNull("DESCRIPTION")


3 comments:

  1. Utility-class psdi.util.StringUtility gives you isEmpty() method that checks if string is empty or null:

    if (!psdi.util.StringUtility.isEmpty(getString("DESCRIPTION"))) {
    ...
    }


    PS: check also com.ibm.tivoli.maximo.util.StringUtil

    ReplyDelete
  2. also an the mbo you should call isNull("YOUR_ATTRIBUTENAME")... it's shorter ;)

    ReplyDelete