Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The following example produces an exception when accessing stage.XMLProperties.Usage.Session.SchemaReconciliation.FailOnTypeMismatch

Code Block
languagegroovy
def sFOTM = " " +  stage.XMLProperties.Usage.Session.SchemaReconciliation.FailOnTypeMismatch.toString() + " ";
if (sFOTM.substring(1,2) == "0") { // schema recon off
  return true
}

The underlying issue is that stage.XMLProperties is null, so evaluating null.Usage triggers a NullPointerException.

NullPointerExceptions A NullPointerException can be prevented by ensuring the code does not access null values. There are scenarios, like the one illustrated above, where the DataStage Designer can produce a Database Connector stage with an incomplete XMLProperties field, which the out0of-the-box MettleCI some of MettleCI’s example Compliance Rules do not handle. Later versions of MettleCI Compliance Rules will handle this situation to make it clear that the Compliance system is behaving as expected, and that the error is caused by an incomplete jobs definitionmay not handle.

There are a few tools available within Compliance Rules which make it easy to avoid this type of problem:

  • Change your graph traversal to ignore stages with a null XMLProperty. e.g. item.graph.V.stage.has('XMLProperties', T.ne, null).???

  • From within a Pipe closure (e.g. sideEffect{}filter{}, etc.), you can use the Groovy safe navigation operator to cancel the evaluation and immediately return null.

    • e.g. stage?.XMLProperties.Usage.Session.SchemaReconciliation.FailOnTypeMismatch.toString() would return null when XMLProperties is null.

  • From within a Pipe closure you can use the Groovy Elvis operator to default a null value to something more usable. Using stage.XMLProperties?:'' will return an empty string whenXMLProperties is null. This could be combined with the Safe Navigation operator described above to produce a reasonable default string:  stage?.XMLProperties.Usage.Session.SchemaReconciliation.FailOnTypeMismatch.toString()?:''

...

The example given above would therefore be better implemented using the following expressions:

Code Block
languagegroovy
def sFOTM = stage?.XMLProperties.Usage.Session.SchemaReconciliation.FailOnTypeMismatch[0]
if (sFOTM == "0") {
   == sFOTMreturn true
}

The example above also appends a space at the start and end of the string, and uses a .toString() and sFOTM.substring(1,2) to strip off the square brackets ([]) which are present when an XML Property is an array. Given that, we’d suggest:

Code Block
def sFOTM = stage?.XMLProperties.Usage.Session.SchemaReconciliation.FailOnTypeMismatch
if (sFOTM == "0") {
   return true
}