cancel
Showing results for 
Search instead for 
Did you mean: 

Injecting UserService yields Null

abbask
Champ on-the-rise
Champ on-the-rise
Hello,

i am trying to inject UserService but it throws NullPointerException when i try to use it.

@Component("createUser")
public class CreateUser implements JavaDelegate{
   private static final Logger LOG = LoggerFactory.getLogger(CreateUser.class);
   //inject user service and group service
   @Autowired
   private UserService userService;
      
   @Autowired
   private GroupService groupService;
   
   public void execute(DelegateExecution execution) throws Exception {
      
      //get process variables
      String userName = (String) execution.getVariable("userName");
      String userEmail = (String) execution.getVariable("userEmail");
      Long userId = (Long) execution.getVariable("userId");
      
      System.out.println(userService + "<<<>>>" + groupService);
      LOG.info(userService + " is userService object");
      //create new user
      User user = userService.createNewUser(userEmail, userName, "", userId.toString(), "TCS", UserStatus.ACTIVE, AccountType.ENTERPRISE, userId);
      userService.save(user);
   }
}

gives null pointer exception at userService.createNewUser(..)


thanks in advance
2 REPLIES 2

vasile_dirla
Star Contributor
Star Contributor
are you sure you use this delegate  like that: ${createUser}

if you use it by filling the "activiti:class" in the service task then a new instance will be created at runtime and the spring beans will not be injected.
ex:
<code>
<serviceTask id="javaService"
             name="My Java Service Task"
             activiti:class="org.activiti.MyJavaDelegate" />
</code>

you should use:
delegateExpression if you want to have @Autowired beans.
<code>
<serviceTask id="serviceTask" activiti:delegateExpression="${delegateExpressionBean}" />
</code>

more details here: http://www.activiti.org/userguide/#bpmnJavaServiceTask

abbask
Champ on-the-rise
Champ on-the-rise
Hi vasila,
Thanks for your reply
I used expression and it worked