Nullpointer problem

Hi everyone!

I'm having an issue with a code. And I can't get where is the problem. The problem is that at the execution it throws me a 'Nullpointer' error. And nothing else. The code is in the following text bloc:

import groovy.sql.Sql

<p>
	def sqlw = BonitaSql.newInstance("jdbc:sqlserver://172.16.0.36;databasename=base", "user", "pass", new com.microsoft.sqlserver.jdbc.SQLServerDriver())<br />
	def dt_inicio = ""<br />
	def dt_fim = ""</p>

<p>
	sqlw.eachRow 'select DATEADD(DAY, DATEDIFF(DAY, 0, (GETDATE()-2)),0) as tempo', {dt_inicio = it.tempo.toString()}<br />
	sqlw.eachRow "select DATEADD(DAY, DATEDIFF(DAY, '19000101', GETDATE()-2), '23:59:59') as tempoz", {dt_fim = it.tempoz.toString()}<br />
	sqlw.close()</p>

<p>
	def sql = BonitaSql.newInstance("jdbc:sqlserver://172.16.0.36;databasename=base", "user", "pass", new com.microsoft.sqlserver.jdbc.SQLServerDriver())<br />
	def queryc = "create table ##tempoh(empresa varchar(10), cliente varchar(10), produto varchar(20), quantidade decimal(14,4),valor int, movimento char(2), documento varchar(20), unidade char(2), movimento_b char(1), cc varchar(10), sistema datetime, data_movimento datetime, ano_movimento int, lote int, ano_lote char(4), usuario char(10), lote_prod char(10), data_prod nvarchar(10), ordem int, ano_op int, item_movim varchar(10), dt_gerado datetime, dt_atuest datetime, dt_atucus datetime) insert into ##tempoh EXEC P_PRODUCAO_SAIDA '${dt_inicio}', '${dt_fim}' "<br />
	sql.execute(queryc)<br />
	def queryB = "create table ##totaisb(linha bigint, produto varchar(20), ano_movimento int, cc varchar(10), data_prod nvarchar(10), quantidade decimal(14,4)) insert into ##totaisb Select row_number() over(order by produto) as linha, produto, cc, ano_movimento, max(data_prod) as data_producao , sum(quantidade) as total from ##tempoh where movimento_b = 'E' group by produto, ano_movimento, cc order by produto"<br />
	sql.execute(queryB)</p>

<p>
	for (int i=1; i&lt;= 100; i++){<br />
	def aux = i<br />
	def produto = ""<br />
	def ano_movimento = 0<br />
	def data_prod = ""<br />
	def quantidade = 0<br />
	def nr_lanmov = ""<br />
	def vl_estatual = 0<br />
	def cci = ""<br />
	def cc = 0</p>

<p>
	sql.eachRow "select produto from ##totaisb where linha= '${aux}' ", {produto += it.produto}<br />
	sql.eachRow "select ano_movimento from ##totaisb where linha= '${aux}' ", {ano_movimento += it.ano_movimento}<br />
	sql.eachRow "select data_prod from ##totaisb where linha= '${aux}' ", {data_prod += it.data_prod}<br />
	sql.eachRow "select quantidade from ##totaisb where linha= '${aux}' ", {quantidade += it.quantidade}<br />
	sql.eachRow "select cc as cci from ##totaisb where linha= '${aux}' ", {cci += it.cci}<br />
	cc = cci.toInteger()</p>

<p>
	&nbsp;</p>

<p>
	Sql sqlb = BonitaSql.newInstance("jdbc:oracle:thin:@192.168.0.18:1521:base", "user", "pass",new oracle.jdbc.OracleDriver())<br />
	sqlb.eachRow "select max(nr_lanmov) + 1&nbsp; as nr_lanmov from base.cemovime where ch_produt = '${produto}' ", {nr_lanmov +=it.nr_lanmov}<br />
	sqlb.eachRow "select max(vl_estatual) as vl_estatual from base.cemovime where ch_produt = '${produto}'", {vl_estatual += it.vl_estatual}<br />
	vl_estatual = vl_estatual+quantidade<br />
	def query = "INSERT INTO base.CEMOVIME VALUES(100,null,'${produto}',${quantidade},0,21,000000,72,null,'E',99999999,9999,'9',2008,sysdate,sysdate-1,${vl_estatual},0,null,null,null,null,null,'${ano_movimento}',null,null,null,null,${nr_lanmov},null,1,'L',null,sysdate-1,null,null,null,1,null,null,null,null,null,null,null,null,null,null,sysdate,sysdate,sysdate)"<br />
	sqlb.execute(query)<br />
	sqlb.close()<br />
	&nbsp;&nbsp; &nbsp;<br />
	}</p>

 

I'm accepting any tip.

Thanks in advance.

Hi Rafael,

the groovy interpreter, as called by the BonitaSoft engine is not the ideal medium for running complex tasks - i usually recommend that long or complex groovy be written in an external console, which has better debugging capability.

That is not always easy/possible, however, when we have bonita process variables or built in values / variables, or sometimes libraries that are a part of the script. When that happens, we can resort to the ancient debugging methods of writing to the log file, e.g.

import java.util.logging.Logger;

Logger log = Logger.getLogger(this.getClass().toString());

log.severe(activityInstance.getUUID().toString() + "DEBUG myVarName value: " + myVarName);

or including error handling via try catch loops in the code. If you add some debug lines of code to your script, you will be able to view those log messages in the bonita.log of the application server, and that will help you locate which groovy object is not being instantiated properly.

-Declan

Hi Declan!

Thanks for your answer.

I kinda realized that groovy interpreter is ~limitated~ at some points. So I just changed my code and some operations. However, I'll try to add some debug code to see what is wrong and what can be done to improve that.

Thanks a lot for your answer.