[rules-users] not able to use map as global variable to get the object

Avin abhijeet.b.patil at jpmorgan.com
Wed Feb 12 12:30:46 EST 2014


Hi,
I know some questions are raised regarding using map in drools. But most of
them have answered that Map to be added as part of working memory and not
global. 

My question is based on same lines. I have global variable of the the type
map. I insert my facts in working memory. This facts are basically keys for
the entities in map. Map will contain elements whose keys will be met or
not.
If element in map is found then i have to assign it to the variable so that
i can use it ahead. I have tried may ways but i didnt get how to work with
it. Similiarly i have multiple maps of different elements and i have to be
sure which map to be used to retrieve element hence i dont want to set map
in working memory.
Below is the *trace of code* that fires Null pointer exception

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import domainObjects.ConcPercentRef;
import domainObjects.EncapsulatingObject;
global java.util.Map encapsulationMap;
rule "test rule"
no-loop
when
$key : EncapsulatingObject(id != 0 )
$object : *EncapsulatingObject(encapsulationMap[$key]* != null)
then
System.out.println("value is  :::  "+encapsulationMap.get($key));
end
*Exception trace *
Exception in thread "main" [Error: null pointer: encapsulationMap[$key]]
[Near : {... encapsulationMap[$key] != null ....}]
             ^
[Line: 1, Column: 1]
	at
org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain

i have also tried using Map(encapsulationMap[$key] !=null but it fails. It
does not throws exception but it dosent enters in then clause because some
of the keys are present. Be sure of that.
Any help will be highly appreciated


Thanks 
Avin




--
View this message in context: http://drools.46999.n3.nabble.com/not-able-to-use-map-as-global-variable-to-get-the-object-tp4028099.html
Sent from the Drools: User forum mailing list archive at Nabble.com.


More information about the rules-users mailing list