cancel
Showing results for 
Search instead for 
Did you mean: 

NullPointerException when native query with null result

lotzy
Champ in-the-making
Champ in-the-making
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.
2 REPLIES 2

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi.

I have implemented small jUnit test (https://github.com/martin-grofcik/Activiti/commit/b311ba6c8729633c17df1d33ad42288b9ac386ee)
<code>
    @Deployment(resources = {"org/activiti/engine/test/history/oneTaskProcess.bpmn20.xml"})
    public void testNativeHistoricProcessInstanceQuery() {
        NativeHistoricProcessInstanceQuery haiq = historyService.createNativeHistoricProcessInstanceQuery();
        HistoricProcessInstance processNative = haiq.sql("SELECT MAX(id_) AS id FROM act_hi_procinst WHERE business_key_ = 'DOES_NOT_EXIST'").singleResult();
        assertEquals(null, processNative);
    }
</code>

It passes. May be I did something wrong.

Question
Could you create jUnit test for it?

Idea
Can you query DB to get rows count before SELECT MAX(id_) ? (I know it is overhead.)

lotzy
Champ in-the-making
Champ in-the-making
Thanks a lot Martin, for your fast response. We ran the JUnit test with our Activiti version 5.12.1 and we got the same NullPointerException, then we upgraded to version 5.13 and we did NOT encounter the error. So this bug is resolved in the latest version of Activiti. Case closed.

Thank you again.