NullPointerException on getCommands()

68 views
Skip to first unread message

Salameh Haj ali

unread,
Feb 19, 2023, 6:01:54 AM2/19/23
to Drools Usage
The following lines are part of my code : 

if (kieRuleSession != null) {
if (kieRuleSession.getKieContainer() != null) {
KieSession kieSession = kieRuleSession.getKieContainer().newKieSession();
kieSession.getAgenda().getAgendaGroup(agenda).setFocus();
List<Command> commands = new ArrayList<>();
KieCommands kieCommands = kieRuleSession.getKieServices().getCommands();

for (Object entity : entities) {
commands.add(kieCommands.newInsert(entity));
}

this line { KieCommands kieCommands = kieRuleSession.getKieServices().getCommands();}  returns null , it's not supposed to return null. However, It only happened with me when I updated drools from version 7.35.0.Final to version 8.33.0.Final, anyone can help me ? you can share with me the way of defining the objects (if there are any new things in the newer version) because i am not a drools expert and my task is to update all dependencies. 
Regards.

Toshiya Kobayashi

unread,
Feb 19, 2023, 10:11:19 PM2/19/23
to Drools Usage
Maybe you are missing a dependency "org.drools:drools-commands"?

In drools 8 (actually, since 7.45), we recommend "engine" dependency which aggregates necessary dependencies.

https://docs.drools.org/latest/drools-docs/docs-website/drools/KIE/index.html#_enginedependency

If you are not yet confortable with executable-model, please use "drools-engine-classic" (which contains "drools-commands", too) and check if the issue is resolved.

```
    <dependency>
      <groupId>org.drools</groupId>
      <artifactId>drools-engine-classic</artifactId>
    </dependency>
```

Regards,
Toshiya


2023年2月19日日曜日 20:01:54 UTC+9 shaja...@gmail.com:

Salameh Haj ali

unread,
Feb 20, 2023, 4:00:50 AM2/20/23
to Drools Usage
I imported the dependency 'drools-engine' but it still returning null value , I am really confused from this error 
anyone have an idea ?

Toshiya Kobayashi

unread,
Feb 20, 2023, 9:01:51 PM2/20/23
to Drools Usage
Could you share a reproducer (a simple maven project to reproduce the error) for investigation?

If it takes time, sharing a source code of `kieRuleSession` class (which seems to be a class of your application, not Drools class) might help. But I may end up asking for a reproducer later.

Regards,
Toshiya

2023年2月20日月曜日 18:00:50 UTC+9 shaja...@gmail.com:

Salameh Haj ali

unread,
Feb 21, 2023, 4:46:41 AM2/21/23
to Drools Usage
Nothing special is in my kieRuleSession class is added 

I have these members 

private final KieServices kieServices;
private KieFileSystem kieFileSystem;
private KieContainer kieContainer;

this constructor 
public KieRuleSession()
{
this.kieServices = KieServices.Factory.get();
this.kieFileSystem = this.getKieServices().newKieFileSystem();
}

and getter for them , i have a function that reads the rules using JPA and writes it in the filesystem 

But, as I noticed today, the function 'getCommands()' inside KieServices, is changed in the latest versions to this: 

public KieCommands getCommands() {
return (KieCommands)KieService.load(KieCommands.class);
}

In the older versions like 7.73.0.Final or 8.13.0.Beta it was just returning a new instance of a class CommandFactoryServiceImpl and the instance is created without any errors 
in the latest , load function is returning null only.

Toshiya Kobayashi

unread,
Feb 21, 2023, 10:09:48 PM2/21/23
to Drools Usage
Oh, I reproduced the issue on my end and sorry about the imprecise information.

"drools-engine-classic" includes "drools-commands", but "drools-engine" doesn't.

If you use "drools-engine", please explicitly add a dependency "drools-commands" as well.

    <dependency>
      <groupId>org.drools</groupId>
      <artifactId>drools-commands</artifactId>
    </dependency>

I filed a JIRA for "drools-engine" to include "drools-commands" for future versions.

https://issues.redhat.com/browse/DROOLS-7349


> But, as I noticed today, the function 'getCommands()' inside KieServices, is changed in the latest versions to this:
>
> public KieCommands getCommands() {
>   return (KieCommands)KieService.load(KieCommands.class);
> }

Yes, it loads KieCommands implementation using ServiceLoader. If drools-commands jar is on the classpath, it should load the impl --- "CommandFactoryServiceImpl".

If you still have the issue, please share a reproducer.

Thanks,
Toshiya

2023年2月21日火曜日 18:46:41 UTC+9 shaja...@gmail.com:
Reply all
Reply to author
Forward
0 new messages