Support Questions

Find answers, ask questions, and share your expertise

Getting NullPointerException while calling Store Procedure in Groovy Script using NiFi. How can I fix this error

avatar
Contributor

I am using ExecuteScript Processor to call the Store procedure. Below is the Groovy Script for the same.

import org.apache.commons.io.IOUtils
import org.apache.nifi.controller.ControllerService
import org.apache.nifi.processor.io.StreamCallback
import java.nio.charset.*
import groovy.sql.OutParameter
import groovy.sql.Sql
import java.sql.ResultSet
import java.sql.Clob
import static java.sql.Types.CLOB
import java.nio.charset.StandardCharsets
import groovy.json.*
def lookup = context.controllerServiceLookup
def dbServiceName = ConncationPool.value
def dbcpServiceId = lookup.getControllerServiceIdentifiers(ControllerService).find {
cs -> lookup.getControllerServiceName(cs) == dbServiceName
}
def conn = lookup.getControllerService(dbcpServiceId).getConnection();
def flowFile = session.get()
if(!flowFile) return
try {
sql = Sql.newInstance(conn);
flowFile = session.write(flowFile,
{ inputStream, outputStream ->
def inputData = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
String sqlString ="""{call MY_PROC(?,?)}""";
def out_res

def parametersList = [inputData,Sql.CLOB];

sql.call(sqlString, parametersList) {out_json_response ->
out_res = out_json_response?.characterStream?.text
};
def json = JsonOutput.toJson(out_res)
def parser = new JsonSlurper()
outputStream.write(parser.parseText(json).getBytes(StandardCharsets.UTF_8))
} as StreamCallback)
conn?.close()
sql.close();
session.transfer(flowFile, REL_SUCCESS)
} catch(Exception e) {
log.error('Error during JSON operations', e)
session.transfer(flowFile, REL_FAILURE)
}
finally {
if (conn != null) conn.close();
if (sql != null) sql.close();
}
conn?.close()
sql.close();

But I am getting Error "during JSON operations: java.lang.NullPointerException: Cannot invoke method getBytes() on null object while calling store procedure from groovy script" Can you please help me what wrong I am doing here. I am new to the Nifi and groovy. Appreciate your help. Thank you

 

0 REPLIES 0