I am new to Groovy and was trying to write a statement to return a row of data from the table F03B13. However, I am encountering an error. I think the error comes from initializing Sql sql but I am not really sure how to solve it.
{ "message": { "BatchGroovy": { "exception": "NullPointerException", "timeStamp": "2021-03-02T12:03:17.634+0800" } }, "exception": "Exception", "timeStamp": "2021-03-02T12:03:17.635+0800", "userDefinedErrorText": "", "status": "ERROR", "exceptionId": "7b706300-cafa-4316-b818-af0619d205fe" }
import groovy.transform.CompileStatic;
import com.oracle.e1.common.OrchestrationAttributes;
import java.text.SimpleDateFormat;
import java.sql.*;
import groovy.sql.Sql;
import groovy.json.JsonOutput;
@ CompileStatic
HashMap < String, Object > main(OrchestrationAttributes orchAttr, HashMap inputMap) {
orchAttr.writeWarn("START DB Groovy");
Sql sql;
String VAPOST = "D";
String VACO = "00013";
String VAGLBA = "21237218";
def rows = sql.rows("SELECT RYICU FROM CRPDTA.F03B13 where RYPOST = '${VAPOST}'AND RYCO = '${VACO}' AND RYGLBA = '${VAGLBA}' ");
HashMap <String,Object> returnMap = new HashMap <String,Object> ();
try {
def jsonArray = JsonOutput.toJson(rows);
returnMap.put("rows", jsonArray);
} catch (Exception e) {
orchAttr.writeWarn("try to put rows in json", e);
}
orchAttr.writeWarn("END DB Groovy");
return returnMap;
}