Wolfgang is right, as usual. Also, if I am not mistaken, if you are using the MVEL you can use ? to prevent the NPE. Something like:

person?.name?.firstName

   But this needs to be double checked. I never used it myself.

   []s
   Edson

2009/6/13 Wolfgang Laun <wolfgang.laun@gmail.com>
You have answered your question yourself: evaluation of patterns is
done whenever a fact is added; salience will only order firings. Thus,
the access person.name.firstName runs into the NPE.

One way to cope with this would be to introduce a Name object which
represents "no name", available as a public static object in Name; use
this to initialize Person objects. This will still let you detect the
absence of a name (name == Name.noName) but it won't run into the NPE
in your second rule.

-W

On 6/12/09, surya_n2007 <surya_n2007@yahoo.co.in> wrote:
>
> Throwing runtime exception in this example inserted person object as person
> name is null. Ideally need to add error
> "P01" and in second rule checks for precondition "P01" exist and skip rule.
> But i am getting the below error.
> Please let me know this is bug, i dont want to check again "Second Rule"
> name object is not null.
>  I think this is happening since drools frist try to run all rules before
> start execution.
>
> please let me know whether anything wrong on my end.
>
>
> Rule Engine COde :
>
>    Person person = test.new Person();
> // Name name = person.new Name();
> // person.setName(name);
>    workingMemory.insert(person);
>
>
> rule "First Rule" salience 100 lock-on-active true
> when
>         person : Person()
>         eval(person.name == null)
> then
>         person.errors.add("P01");
>         System.out.println("Rule 1 invoked"+person.errors);
>         update(person);
> end
>
> rule "Second Rule" salience 99 lock-on-active true
> when
>         person : Person()
>         eval(!person.errors.contains("P01"))
>         eval(person.name.firstName=="Bob")
> then
>         person.name.suffix="Mr";
>         System.out.println("Rule 2 invoked");
> end
>
>
>
>
> org.drools.RuntimeDroolsException:
> com.sample.Rule_Second_Rule_0Eval1Invoker@4b42aa1e :
> java.lang.NullPointerException
>         at org.drools.rule.EvalCondition.isAllowed(EvalCondition.java:82)
>         at
> org.drools.reteoo.EvalConditionNode.assertTuple(EvalConditionNode.java:148)
>         at
> org.drools.reteoo.SingleTupleSinkAdapter.propagateAssertTuple(SingleTupleSinkAdapter.java:29)
>         at
> org.drools.reteoo.EvalConditionNode.assertTuple(EvalConditionNode.java:157)
>         at
> org.drools.reteoo.CompositeTupleSinkAdapter.createAndPropagateAssertTuple(CompositeTupleSinkAdapter.java:73)
>         at
> org.drools.reteoo.LeftInputAdapterNode.assertObject(LeftInputAdapterNode.java:116)
>         at
> org.drools.reteoo.SingleObjectSinkAdapter.propagateAssertObject(SingleObjectSinkAdapter.java:22)
>         at
> org.drools.reteoo.ObjectTypeNode.assertObject(ObjectTypeNode.java:162)
>         at org.drools.reteoo.Rete.assertObject(Rete.java:175)
>         at
> org.drools.reteoo.ReteooRuleBase.assertObject(ReteooRuleBase.java:192)
>         at
> org.drools.reteoo.ReteooWorkingMemory.doInsert(ReteooWorkingMemory.java:71)
>         at
> org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:911)
>         at
> org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:883)
>         at
> org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:684)
>         at com.sample.DroolsTest.main(DroolsTest.java:36)
> Caused by: java.lang.NullPointerException
>         at com.sample.Rule_Second_Rule_0.eval1(Rule_Second_Rule_0.java:14)
>         at
> com.sample.Rule_Second_Rule_0Eval1Invoker.evaluate(Rule_Second_Rule_0Eval1Invoker.java:20)
>         at org.drools.rule.EvalCondition.isAllowed(EvalCondition.java:77)
>         ... 14 more
> --
> View this message in context:
> http://www.nabble.com/Throwing-runtimeException-4.0.7-not-supposed-tp24005070p24005070.html
> Sent from the drools - dev mailing list archive at Nabble.com.
>
> _______________________________________________
> rules-dev mailing list
> rules-dev@lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-dev
>
_______________________________________________
rules-dev mailing list
rules-dev@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-dev



--
 Edson Tirelli
 JBoss Drools Core Development
 JBoss, a division of Red Hat @ www.jboss.com