8 Replies Latest reply on Jul 25, 2007 9:14 AM by massimiliano_cuccia

    create task at runtime

      In a process I defined an handler for a transition.
      This handler should create a new task, this is the code of the handler:

      TaskInstance newti = executionContext.getProcessInstance().getTaskMgmtInstance().createTaskInstance();
      newti.setName("created at runtime");
      newti.addComment("created after the task named 'definition1' .");
      newti.setActorId("admin");
      newti.setVariable("sample_var", "12345");
      


      so, when I try to execute this task (and even other tasks) I obtains a null pointer exception executing this code

      TaskController taskController = ti.getTask().getTaskController();


      I'm using this code to obtains the list of variables for the executing task (so this list should contain only "sample_var" in this example)

      Now, am I wrong on task creation code?
      or maybe have I to retrieve the list of variables in another way?

      thanks for your help
      regards

        • 1. Re: create task at runtime

          Hi,

          your taskInstance is named newti or u r calling ti.getTaskController () ...

          Regards,

          O.M.

          • 2. Re: create task at runtime

            the first portion of code is from the handler, the name for the newly created task is newti
            the second portion of code is from the struts action to execute a generic task, that i'm using to execute this task also. In this fragment the name of the task instance is ti.

            It isn't a java mistake ... be sure!

            • 3. Re: create task at runtime
              syngolis

              I think the null pointer exception is correct. AFAIK if you create a task, there is no task controller created automatically. (Think of a processdefinition; there you have to declare the controller as well)
              And the get operation results in a null pointer because no controller exists.

              • 4. Re: create task at runtime

                 

                "syngolis" wrote:
                I think the null pointer exception is correct. AFAIK if you create a task, there is no task controller created automatically. (Think of a processdefinition; there you have to declare the controller as well)
                And the get operation results in a null pointer because no controller exists.


                thanks syngolis
                I think that you are right: it is possible.

                So I want to list all and only the variables linked to the task (not the "all task of the process"). At now I'm doing this

                TaskController taskController = ti.getTask().getTaskController();
                if (taskController!=null)
                {
                 List variableAccesses = taskController.getVariableAccesses();
                 Iterator i = variableAccesses.iterator();
                 while (i.hasNext())
                 {
                 // do something
                 }
                }


                but the first row gives the null pointer exception (this means that ti.getTask() is null)

                so if you are right, the question is
                how to list the variables of the task?

                regards

                • 5. Re: create task at runtime
                  syngolis

                   

                  "massimiliano_cuccia" wrote:
                  (this means that ti.getTask() is null)


                  No, this means getTaskController() is null, as I mentioned in the post before. Try it without the controller or create one.

                  In order to get variables (without variableaccess):
                  taskInstance.getVariable("Name")


                  For further information, please have a look at the testcases. There are much examples.

                  • 6. Re: create task at runtime

                     

                    "syngolis" wrote:
                    "massimiliano_cuccia" wrote:
                    (this means that ti.getTask() is null)


                    No, this means getTaskController() is null,

                    I'm sorry, but you are wrong

                    to prove this I added these few lines
                    System.out.println( "ti: " + ti );
                    System.out.println( "ti.getTask(): " + ti.getTask() );
                    System.out.println( "ti.getTask().getTaskController(): " + ti.getTask().getTaskController() );
                    


                    and obtained this output
                    ti: TaskInstance[creato a runtime]
                    ti.getTask(): null
                    java.lang.NullPointerException
                     at it.acme.action.AttivitaEsecuzione.execute(AttivitaEsecuzione.java:71)
                    



                    "syngolis" wrote:

                    Try it without the controller

                    do you know a way to do this without the controller?

                    "syngolis" wrote:

                    or create one.

                    ok, i'm trying this

                    "syngolis" wrote:

                    In order to get variables (without variableaccess):
                    taskInstance.getVariable("Name")


                    but I doesn't know the variable name
                    I'm writing an action to execute a generic task and the variable name is decided at design time!!

                    I'm searching for the testcases
                    thanks

                    any other can help?


                    • 7. Re: create task at runtime
                      syngolis

                      sorry...I was wrong.

                      (You neither have a task, nor a taskcontroller. So both of them would cause the nullpointer.)

                      I am very interested in that issue so i will test myself a little bit. I found a testcase called: RuntimeTaskCreationTest. Maybe that helps.

                      • 8. Re: create task at runtime

                        FIXED!!!

                        I changed some things
                        I attached the handler on node leave
                        in the handler I created the new task and created a new variable using

                        tCompDoc.setVariableLocally(varname, varvalue);


                        then in the action to execute a task I called
                        ti.getVariablesLocally()

                        to obtains the list of variables defined in the executing task

                        now I doesn't care about ti.getTask that is null
                        i doesn't care about a taskcontroller
                        i doesn't care about any other boring stuff

                        thanks to syngolis!

                        If someone has any questions about my code can ask me!

                        regards