setVariable of RunTimeService gives execution of null, when i try to set variable

I am making an API call from service task, inside the rest controller I am trying to store variable using RunTimeService setVariable method, where I am passing process instance id which is execution id and name of variable and value

I get an exception saying [NullValueException: execution doesn’t exist: execution is null]

runTimeService.setVariable()

Can you add your full code?

@Autowired

RuntimeService runTimeService;

@GetMapping(value="/hello")

public String sayHello() {

return “hello!!!”;

}

@PostMapping(value="/add-call-observer")

public Boolean createOrAddObserver(@RequestBody TestDto test) throws Exception{

runTimeService.setVariable(test.getExecutionId(), “test”, “test”);

return true ;

}

test.getExecutionId() must be reference to the path of execution or null if it is not related to a process instance.

In your case, value of test.getExecutionId() doesn’t match the reference to the path of execution. So it throws NullValueException.