Drools version 6.0.0.FINAL
I create rule package in Drools workbench and use default knowledgebase and
session:
KieServices kieServices = KieServices.Factory.get();
KieContainer kContainer =
kieServices.newKieContainer(kieServices.newReleaseId("com.test",
"validationrules", "1.0-SNAPSHOT"));
KieBase kbase = kContainer.getKieBase();
KieSession ksession = kContainer.newKieSession();
I have a query that returns ValidationError:
query "getValidationErrors"
validationError: ValidationError()
end
ValidationError is returned and it has values in its fields, but when i try
to read fields of that ValidationError fact, i get NullPointerException
public class DroolsValidationMessage {
private FactType factType;
private Object fact;
public DroolsValidationMessage(KieBase kbase, Object fact) {
this.fact = fact;
factType = kbase.getFactType(fact.getClass().getPackage().getName(),
fact.getClass().getSimpleName());
}
public String getSeverity() {
return (String) factType.get(fact, "severity"); // <--
java.lang.NullPointerException
}
public String getDescription() {
return (String) factType.get(fact, "description");
}
}
factType looks like this:
ClassDefinition{className='null', superClass='java.lang.Object',
interfaces=[], definedClass=class com.test.validationrules.ValidationError,
traitable=null, abstract=false,
fields={description=FieldDefinition{name='description',
type='java.lang.String', key=false, inherited=false, index=0,
initExpr='null', annotations=null, accessor=null},
severity=FieldDefinition{name='severity', type='java.lang.String',
key=false, inherited=false, index=1, initExpr='null', annotations=null,
accessor=null}
This works if the rules and declared class is in resources folder and
kmodule.xml is used.
KieServices kieServices = KieServices.Factory.get();
KieContainer kContainer = kieServices.getKieClasspathContainer();
KieBase kbase = kContainer.getKieBase("kbase");
KieSession ksession = kContainer.newKieSession("ksession");
factType looks like this:
ClassDefinition{className='com.test.validationrules.ValidationError',
superClass='java.lang.Object', interfaces=[java.io.Serializable],
definedClass=class com.test.validationrules.ValidationError, traitable=NONE,
abstract=false, fields={description=FieldDefinition{name='description',
type='java.lang.String', key=false, inherited=false, index=0,
initExpr='null', annotations=null, accessor=[ClassFieldExtractor
class=com.test.validationrules.ValidationError field=description]},
severity=FieldDefinition{name='severity', type='java.lang.String',
key=false, inherited=false, index=1, initExpr='null', annotations=null,
accessor=[ClassFieldExtractor class=com.test.validationrules.ValidationError
field=severity]}
-Jarkko
--
View this message in context:
http://drools.46999.n3.nabble.com/NullPointerException-with-factType-get-...
Sent from the Drools: User forum mailing list archive at
Nabble.com.