cancel
Showing results for 
Search instead for 
Did you mean: 

NPE on variables get from ProcessInstance loaded via ProcessInstanceQuery

mmaker1234
Champ in-the-making
Champ in-the-making
Hello Activiti developers,

While executing in Activiti 5.13<java start="488" fancy="6">  ProcessInstance process = runtimeService.createProcessInstanceQuery()
                                    .processInstanceId( "1234" )
                                    .includeProcessVariables()
                                    .singleResult();
  if( process != null ) {
    Map<String, Object> vars = process.getProcessVariables(); //Throws NPE in ByteArrayRef.java:94
    …
  }</java>I observe the following error
java.lang.NullPointerException
    at org.activiti.engine.impl.persistence.entity.ByteArrayRef.ensureInitialized(ByteArrayRef.java:94)
    at org.activiti.engine.impl.persistence.entity.ByteArrayRef.getBytes(ByteArrayRef.java:45)
    at org.activiti.engine.impl.persistence.entity.VariableInstanceEntity.getBytes(VariableInstanceEntity.java:138)
    at org.activiti.engine.impl.variable.ByteArrayType.getValue(ByteArrayType.java:32)
    at org.activiti.engine.impl.variable.SerializableType.getValue(SerializableType.java:51)
    at org.activiti.engine.impl.persistence.entity.VariableInstanceEntity.getValue(VariableInstanceEntity.java:165)
    at org.activiti.engine.impl.persistence.entity.ExecutionEntity.getProcessVariables(ExecutionEntity.java:1384)
    at org.activiti.engine.impl.ant.ShowProcessState.formatProcessInstance(ShowProcessState.java:493)

I tried to debug and found that
Context.getCommandContext()
returns
null
in
ByteArrayRef.java:94
.

A similar code that passes the same row (
ByteArrayRef.java:94
) runs fine:<java start="488" fancy="6">  List<Execution> executions = runtimeService.createExecutionQuery()
                                    .processInstanceId( "1234" )
                                    .list();
  if( executions != null ) {
    for (Execution execution : executions) {
      Map<String, Object> globalVars = runtimeService.getVariables( execution.getId() );
      if( globalVars != null ) {
        …
      }
    }
  }</java>

Am I doing something wrong?
8 REPLIES 8

mmaker1234
Champ in-the-making
Champ in-the-making
Hello,

Isn't there anyone to comment on the above?

frederikherema1
Star Contributor
Star Contributor

In case someone wants to follow the github link above (it shouldn't have the trailing ".") click this link instead:
https://github.com/Activiti/Activiti/commit/136859b6e4100d80c34bfc5c43964c8c8e4362de

mmaker1234
Champ in-the-making
Champ in-the-making
Thank you, Frederik! 🙂

I'm glad I helped somehow.

trond_nordli
Champ in-the-making
Champ in-the-making
I have more or less the same bug in version 5.13 of the REST API, where you do not get the variables on the list of processes.

Works on list of task as expected:
/runtime/tasks?includeProcessVariables = true

Does not work on list of process:
/runtime/process-instances?includeProcessVariables = true

Where you only get blank values ​​"variables": []

Will this error be corrected with the other issue or should this issue be reported separately ?

frederikherema1
Star Contributor
Star Contributor
Should be reported separately, as it's not an exception that is occurring but rather something not working as expected…

st1
Champ in-the-making
Champ in-the-making
First of all sorry for my english Smiley Happy.
We use version 5.13 and to get list of HistoricVariableInstance we wanted to use NativeHistoricVariableInstanceQuery.

<code>
public List<HistoricVariableInstance> listHistoricVariableInstances(String processInstanceId, Collection<String> variableNames) {
..
StringBuilder query = new StringBuilder();
  query.append("select * from ");
  query.append(managementService.getTableName(HistoricVariableInstance.class));
  query.append(" where  proc_inst_id_ = #{proc_inst_id} and name_ in (");
  query.append(ActivitiUtils.createInSet(variableNames));
  query.append(")");
  return historyService.createNativeHistoricVariableInstanceQuery().sql(query.toString()).parameter("proc_inst_id", processInstanceId).list();
}
</code>

But when we invoke "HistoricVariableInstance#getValue()",

<code>
private void mapVarList(List<HistoricVariableInstance> historicVariableInstances, Map<String, UserLoopResponses> varMap) {
  if (historicVariableInstances == null || historicVariableInstances.isEmpty()) {
   return;
  }
  for (HistoricVariableInstance hiVariable : historicVariableInstances) {
   Object value = hiVariable.getValue();//This line throws npe

   if (value == null || !(value instanceof UserLoopResponses)) {
    continue;
   }
   varMap.put(hiVariable.getVariableName(), (UserLoopResponses) value);
  }
}
</code>

we get :

<code>
java.lang.NullPointerException
at org.activiti.engine.impl.persistence.entity.ByteArrayRef.ensureInitialized(ByteArrayRef.java:94)
at org.activiti.engine.impl.persistence.entity.ByteArrayRef.getBytes(ByteArrayRef.java:45)
at org.activiti.engine.impl.persistence.entity.HistoricVariableInstanceEntity.getBytes(HistoricVariableInstanceEntity.java:119)
at org.activiti.engine.impl.variable.ByteArrayType.getValue(ByteArrayType.java:32)
at org.activiti.engine.impl.variable.SerializableType.getValue(SerializableType.java:51)
</code>
Historic value is serializable type. Now my question, is this is the same bug (i tried on version 5.14 but still that same) or i can't get serializable value by nativequery
(i'm beginer at activiti so i may have probably doing something silly Smiley Happy )?

trademak
Star Contributor
Star Contributor
The query that you are performing can also be done via the standard Activiti API.
It was a bug in 5.13, but it should be working in 5.14. Could you create a JIRA issue with a unit test showing the issue?

Best regards,