1

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;
}
Synetrix
  • 11
  • 2
  • The full error should tell you the line number where the error occurred. – daggett Mar 02 '21 at 03:58
  • It doesn't mention much about the error but most probably is the line Sql sql. { "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" } – Synetrix Mar 02 '21 at 04:07
  • The Groovy here is just syntactic sugar, and you're actually writing almost exactly plain Java. The same explanation applies. Note that your code would be a lot simpler eliminating `returnMap` and using `return [rows: jsonArray]` and `return Collections.emptyMap()`. – chrylis -cautiouslyoptimistic- Mar 02 '21 at 05:30

0 Answers0