'Getting null pointer exception while running helloworld in drools
I got the following error while running a simple helloworld sample drools project.
199 [main] ERROR org.drools.compiler.kie.builder.impl.KieContainerImpl - Unknown KieSession name: ksession-rules
java.lang.NullPointerException
at com.sample.DroolsTest.main(DroolsTest.java:24)
Code:
package com.sample;
import org.kie.api.KieServices; import
org.kie.api.runtime.KieContainer; import
org.kie.api.runtime.KieSession;
/** * This is a sample class to launch a rule. */ public class
DroolsTest {
public static final void main(String[] args) {
try {
// load up the knowledge base
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
KieSession kSession = kContainer.newKieSession("ksession-rules");
// go !
Message message = new Message();
message.setMessage("Hello World");
message.setStatus(Message.HELLO);
kSession.insert(message);
kSession.fireAllRules();
} catch (Throwable t) {
t.printStackTrace();
}
}
public static class Message {
public static final int HELLO = 0;
public static final int GOODBYE = 1;
private String message;
private int status;
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
public int getStatus() {
return this.status;
}
public void setStatus(int status) {
this.status = status;
}
}
}
drools code:
package com.sample
import com.sample.DroolsTest.Message;
rule "Hello World"
when
m : Message( status == Message.HELLO, myMessage : message )
then
System.out.println( myMessage );
m.setMessage( "Goodbye cruel world" );
m.setStatus( Message.GOODBYE );
update( m );
end
rule "GoodBye"
when
Message( status == Message.GOODBYE, myMessage : message )
then
System.out.println( myMessage );
end
Solution 1:[1]
It looks drools kie-api/internal library execution looks for mandatory values under src\main\resources\META-INF\maven\pom.properties file for the drools eclipse project.
Updating my pom.xml or pom.properties into below content worked fine on 6.0.0 drools distribution.
groupId=com.test.sample.drools
artifactId=DroolsTestProject
version=1
Solution 2:[2]
I recently started with JBPM and ran into a similar problem. I had two issues that needed to be sorted out.
First I need to create the pom.properties file that was listed in Another answer in this thread. I had to create the src/main/resources/META-INF/maven/pom.properties file since it was not automatically generated when I created a new JBPM Maven project through eclipse. The file should have the following information, which should match your project's pom file:
groupId=com.sample
artifactId=jbpm-example
version=1.0.0-SNAPSHOT
The other issue I ran into was having a blank kmodule.xml. This should be in src/main/resources/META-INF/kmodule.xml. You will need the following in the file:
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
<kbase name="process" packages="process">
<ksession name="ksession-process"/>
</kbase>
</kmodule>
If you look at the newKieSession method parameter, it need to match the "name" attribute of the ksession element. The "packages" attribute of the kbase element needs to match the package name where you DRL files are located.
Solution 3:[3]
Little late on the answer here, but...
I checked my pom.properties file, and it only contained this:
groupId=
artifactId=
version=
Nothing was defined! Changed it to:
groupId=DroolsTest
artifactId=DroolsTest
version=0.0.1-SNAPSHOT
Taking whatever you have as those properties in pom.xml and putting them in pom.properties fixed my problem. Runs fine now.
Solution 4:[4]
The sample project dependencies are configured using Maven. You need to either run "mvn eclipse:eclipse" on the command line, or install the m2e Eclipse plugin.
Solution 5:[5]
Had the same problem and worked fine after adding .drl file to classpath, as discussed in following thread http://drools.46999.n3.nabble.com/Null-pointer-exception-when-adding-drools-to-existing-project-td4027944.html#a4028011
Solution 6:[6]
check your kmodule.xml You must add the new ksession in there. also when defining a new kbase in that file, make sure the name is different for each kbase and the package name is where the rules are. ksession name is what you specified in the execution when you initiated a ksession.
Solution 7:[7]
https://www.tutorialspoint.com/drools/index.htm
the tutorial works, but it's need to change the drl package
please see the figure below to know how to configure your project
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Saravanan Thangavel |
Solution 2 | Community |
Solution 3 | Andrew |
Solution 4 | Steve |
Solution 5 | kashili kashili |
Solution 6 | Anupam Mahapatra |
Solution 7 | cealex |