cancel
Showing results for 
Search instead for 
Did you mean: 

How to add new user to activiti through API

vishnukumarnekk
Champ in-the-making
Champ in-the-making
I am trrying ti add users to the process engine, but it keeps throwing the java.lang.NullPointerException
Here is my code:

package fluis;

import java.util.List;

import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.FormService;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.identity.*;
import org.activiti.engine.task.Task;

public class adduser {
   protected transient static ProcessEngine processEngine;
   protected transient static IdentityService identityService;
   protected transient static RepositoryService repositoryService;
   protected transient static RuntimeService runtimeService;
   protected transient static HistoryService historyService;
   protected transient static FormService formService;

   public static void initProcessEngineServices(){
      processEngine = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration()
              .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE)
              .setJdbcDriver("org.h2.Driver")
              .setJdbcUrl("jdbc:h2:tcp://localhost/activiti;DB_CLOSE_DELAY=1000")
              .setJobExecutorActivate(true)
              .buildProcessEngine();
       repositoryService = processEngine.getRepositoryService();
       runtimeService = processEngine.getRuntimeService();
       historyService = processEngine.getHistoryService();
   }
   public static void initDeployments(){
       repositoryService.createDeployment()
         .addClasspathResource("processes/createTimersProcess.bpmn20.xml")
         .addClasspathResource("processes/FinancialReportProcess.bpmn20.xml")
         .deploy();
      
   }
   
   public static void newDeployment(String fileName){
      processEngine.getRepositoryService().createDeployment().addClasspathResource("processes/" + fileName).deploy();
   }
   
   public static void createUser(String userId, String firstName, String lastName, String email){
      
       if (identityService.createUserQuery().userId(userId).count() == 0) {
         User user = identityService.newUser(userId);
         user.setFirstName(firstName);
         user.setLastName(lastName);
         user.setPassword("");
         user.setEmail(email);
         identityService.saveUser(user);
       }
      
     }

   public static void main(String[] args) {
      
      initProcessEngineServices();
      initDeployments();
      newDeployment("oneTaskProcess.bpmn20.xml");
      createUser("myid", "hermit", "stone", "kermit@kmail.com");
   //   User x =  identityService.newUser("gty");
    //  System.out.println("asd");
     }
}
4 REPLIES 4

martin_grofcik
Confirmed Champ
Confirmed Champ
Any stacktrace?
Can you create jUniti test?
http://forums.activiti.org/content/sticky-how-write-unit-test

Regards
Martin

Console output:
log4j:WARN No appenders could be found for logger (org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" java.lang.NullPointerException
at fluis.adduser.main(adduser.java:77)


I ran the unit test and it is throwing an Error:
Class not found fluis.MyUnitTest
java.lang.ClassNotFoundException: fluis.MyUnitTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:685)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:421)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

I also tried to catch the exception but the try{} catch(){} method is not working

trademak
Star Contributor
Star Contributor
The identityService attribute seems to be null in your example. I also don't see it getting assigned an IdentityService instance, which you do for the repositoryService etc.

Best regards,

Thank alot, I wouldn't have check it forever.