AnsweredAssumed Answered

NullPointerException when native query with null result

Question asked by lotzy on Oct 1, 2013
Latest reply on Oct 3, 2013 by lotzy
Hi there,

We need to find out the last executed process with a given name so we used this piece of code:

NativeHistoricProcessInstanceQuery haiq = historyService.createNativeHistoricProcessInstanceQuery();
HistoricProcessInstance processNative = haiq.sql("SELECT MAX(id_) AS id FROM act_hi_procinst WHERE business_key_ = '"+processName+"'").singleResult();


and then with the max id we will load the whole HistoricProcessInstanceQuery:

HistoricProcessInstanceQuery hquery = historyService.createHistoricProcessInstanceQuery();
return hquery.processInstanceId(processNative.getId()).singleResult();


The problem is that when the native query of SELECT MAX() has null result because there is no process executed with the given "processName" name, then throws

java.lang.NullPointerException
   at org.activiti.engine.impl.persistence.entity.HistoricProcessInstanceEntityManager.findHistoricProcessInstanceCountByNativeQuery(HistoricProcessInstanceEntityManager.java:99)


and does not returns null as we would expect.

Do you have a workaround for this problem other than catching the NullPointerException in a try-catch block?

Thank you.

Outcomes