Java 在drools中运行helloworld时出现空指针异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20315910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting null pointer exception while running helloworld in drools
提问by Aksh1801
I got the following error while running a simple helloworld sample drools project.
我在运行一个简单的 helloworld 示例 drools 项目时遇到以下错误。
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
采纳答案by Saravanan Thangavel
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.
它看起来 drools kie-api/internal library execution 在 src\main\resources\META-INF\maven\pom.properties 文件下寻找 drools eclipse 项目的强制值。
Updating my pom.xml or pom.properties into below content worked fine on 6.0.0 drools distribution.
将我的 pom.xml 或 pom.properties 更新为以下内容在 6.0.0 drools 发行版上运行良好。
groupId=com.test.sample.drools
artifactId=DroolsTestProject
version=1
回答by Steve
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.
示例项目依赖项是使用 Maven 配置的。您需要在命令行上运行“mvn eclipse:eclipse”,或者安装 m2e Eclipse 插件。
回答by kashili kashili
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
在将 .drl 文件添加到类路径后遇到了同样的问题并且工作正常,如以下线程 http://drools.46999.n3.nabble.com/Null-pointer-exception-when-adding-drools-to-existing- 中所述-项目-td4027944.html#a4028011
回答by ProfVersaggi
There was a very constructive discussion on this topic that pretty much nailed it over here: DISCUSSION.
关于这个话题有一个非常有建设性的讨论,几乎把它固定在这里: 讨论。
回答by Mike
I recently started with JBPM and ran into a similar problem. I had two issues that needed to be sorted out.
我最近开始使用 JBPM 并遇到了类似的问题。我有两个问题需要解决。
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:
首先,我需要创建在该线程的另一个答案中列出的 pom.properties 文件。我必须创建 src/main/resources/META-INF/maven/pom.properties 文件,因为当我通过 Eclipse 创建一个新的 JBPM Maven 项目时它没有自动生成。该文件应包含以下信息,这些信息应与您项目的 pom 文件匹配:
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.xml。这应该在 src/main/resources/META-INF/kmodule.xml 中。您将需要文件中的以下内容:
<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.
如果查看 newKieSession 方法参数,它需要匹配 ksession 元素的“name”属性。kbase 元素的“packages”属性需要匹配 DRL 文件所在的包名称。
回答by Andrew
Little late on the answer here, but...
这里的答案有点晚了,但是......
I checked my pom.properties file, and it only contained this:
我检查了我的 pom.properties 文件,它只包含以下内容:
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.
将您拥有的任何内容作为 pom.xml 中的那些属性并将它们放入 pom.properties 中解决了我的问题。现在运行良好。
回答by Anupam Mahapatra
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.
检查您的 kmodule.xml 您必须在其中添加新的 ksession。同样在该文件中定义新的 kbase 时,请确保每个 kbase 的名称不同,并且包名称是规则所在的位置。ksession 名称是您在启动 ksession 时在执行中指定的名称。