1
votes

I received a NullPointerException when running rules; i have simplified the DRL file to reproduce the problem.

These are my versions:

  • KIE_VERSION:7.53.0.Final
  • jboss/business-central-workbench-showcase:latest
rule "retract"
no-loop
when

then
  retract( new String("myString"))
end

When I fire this rule, Drools throws a NullPointerException.

I have testet it in both equality mode and identity mode.

In equality mode, it fails unless a Fact new String("myString") was previously inserted into working memory.

Is this a bug? How do I avoid it?

Thanks, Saverio

1

1 Answers

1
votes

I'm not sure what you're trying to do in your rule.

The retract action is intended to remove a fact from working memory. In your code, you're trying to remove something that is not in working memory.

Here's a simple example for how this is supposed to work. Let's say we have some rules that key off of an object called MyFact that has some properties. So we have some rules that look like this:

rule "Example rule 1"
when
  MyFact( value == "foo" )
then
  // do something for 'foo'
end

rule "Example rule 2"
when
  MyFact( value == "bar" )
then
  // do something for 'bar'
end

Now, as long as we have a MyFact instance in working memory, these rules will be evaluated. So to keep these rules from firing, we can retract the fact -- but we have to retract the instance.

Example of the retraction:

rule "Retract fact when value is 'abc'"
when
  $f: MyFact( value == "abc" ) // $f is the instance
then
  retract($f)
end

We're not retracting arbitrary data -- we retract the specific fact instance we want to retract.

If I tried to retract something not in working memory -- then the NullPointerException makes total sense. You can't retract something that's not there, so when the Drools engine can't find the thing you've requested it throws the exception.