4 Replies Latest reply on Feb 20, 2013 5:16 AM by haddar

    jBPM 5.2 set process instance variable from java - NullPointerException

    slash17

      Hi to all.

       

      I'm using jBPM for two projects that need a workflow engine.

      In particular I'm just using the jBPM runtime, embedded in our jBoss AS 5.1 web applications.

       

      Thanks to you all I solved the several problems I found in this month (deploy a jBPM based web app in AS 5.1 instead of AS 7, make persistence work with an external oracle DB etc..maybe I will make a discussion with my experience if this can help someone)

       

      But now I face a real strange (and big) problem I can't solve.

       

       

      I need to access and modify process instance variables from my java code, using the API.

       

      Here is a code snippet (just for example):

      WorkflowProcessInstance p = (WorkflowProcessInstance) jBPMSession.getProcessInstance(procId);
      System.out.println("before set: " + p.getVariable("user"));
      p.setVariable("user", "krisv");
      System.out.println("after set: " + p.getVariable("user"));
      

       

       

      I can get the variable value using this code p.getVariable("user")

      but this p.setVariable("user", "krisv"); throws a NullPointerException, cause kruntime is null (:

       

      [....]
      11:20:53,095 INFO  [STDOUT] before set: john
      11:20:53,095 ERROR [[ExampleServlet]] Servlet.service() for servlet ExampleServlet threw exception
      java.lang.NullPointerException
                at org.jbpm.process.instance.impl.ProcessInstanceImpl.getProcess(ProcessInstanceImpl.java:67)
                at org.jbpm.process.instance.impl.ProcessInstanceImpl.getContextInstance(ProcessInstanceImpl.java:127)
                at org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl.setVariable(WorkflowProcessInstanceImpl.java:238)
                at com.valueteam.jbpm.main.JBPMUtil.setVariable(JBPMUtil.java:188)
                at com.valueteam.example.ExampleServlet.doGet(ExampleServlet.java:81)
                at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
                at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
                at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
                at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
                at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
                at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
                at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
                at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
                at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
                at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
                at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
                at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
                at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
                at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
                at java.lang.Thread.run(Thread.java:662)
      
      

       

       

      What is the cause and how can I solve this?

       

      Thanks in advance.

        • 1. Re: jBPM 5.2 set process instance variable from java - NullPointerException
          calca

          Hey, I don't know if it can help, but how is your jBPMSession at this moment? Are you using persistence?Have you loaded it, or just created it? Maybe this instance is disposed and you need to load it again? (In case you are using persistence).

           

          Demian

          • 2. Re: jBPM 5.2 set process instance variable from java - NullPointerException
            slash17

            Thanks for the answer.

             

            My jBPMSession is of course active....I instantiate it in in the init() method of my startup servlet and dispose it in it's destroy method.

            Moreover I'm sure it's active because I can start new processes without problems.

             

            I tried both creating it (using newStatefullKnoledgeSession) and load it from db (using loadStatefullKnoledgeSession)...nothing changes.

            Moreover, I'm trying to access and modify a variable af a process just created in this session, not in a previous one, so I don't think that creating o loading it could be the problem.

             

             

            Yet another code snippet to better explain my situation:

             

            Map<String, Object> vars = new HashMap<String, Object>();
            vars.put("user", "john");
            ProcessInstance processInstance = jBPMSession.startProcess("MyProcess", vars);
            // the process starts correctly
            WorkflowProcessInstance p = (WorkflowProcessInstance) processInstance;
            System.out.println("before set: " + p.getVariable("user"));
            // it works and print "bofore set: john" as you can see in the first post
            p.setVariable("user", "krisv");
            // this throws the NullPointerException...
            
            • 3. Re: jBPM 5.2 set process instance variable from java - NullPointerException
              slash17

              I found a solution.

               

              The problem is that I was not using transactions...

               

              Just surround my code in a tm.begin() and tm.commit() and it works fine.

               

              I get my tm using the jboss tm locator:

              TransactionManager tm = TransactionManagerLocator.locateTransactionManager();
              tm.begin();
              // get and set variables here
              tm.commit();
              

               

              Is this the right way to go?

              1 of 1 people found this helpful
              • 4. Re: jBPM 5.2 set process instance variable from java - NullPointerException
                haddar

                Thank you Salvatore Loria. I have got the same problem. when I use procInstance.getVariable(varName) it works fine without a transaction. but when setting a var, it abviously needs a transaction.