1 Reply Latest reply on Dec 21, 2010 9:22 AM by wdfink

    Null pointer Exception

    roxy1987

      Hi

      I am trying to create a class that has a function that takes the rule resource name and the facts as arguments and fire the rules.

      The class is as follows

       

      package com.sample;

       

      import java.io.InputStreamReader;

       

      import org.drools.RuleBase;
      import org.drools.RuleBaseFactory;
      import org.drools.WorkingMemory;
      import org.drools.compiler.PackageBuilder;
      import org.drools.rule.Package;

       

      @SuppressWarnings("restriction")
      public class RuleRunner {

       

          public RuleRunner() {
          }

       

          public void runRules(String[] rules,
                               Object[] facts) throws Exception {

       

              RuleBase ruleBase = RuleBaseFactory.newRuleBase();
              PackageBuilder builder = new PackageBuilder();

       

              for ( int i = 0; i < rules.length; i++ ) {
                  String ruleFile = rules[i];
                  System.out.println( "Loading file: " + ruleFile ); 
                  builder.addPackageFromDrl(new InputStreamReader( RuleRunner.class.getResourceAsStream(ruleFile ) ) );
              }
              Package pkg=builder.getPackage();
              ruleBase.addPackage( pkg );
              WorkingMemory workingMemory = ruleBase.newStatefulSession();

       

              for ( int i = 0; i < facts.length; i++ ) {
                  Object fact = facts[i];
                  System.out.println( "Inserting fact: " + fact );
                  workingMemory.insert( fact );
              }

       

              workingMemory.fireAllRules();
          }
      }

       

      The other class that calls this function is following :

       

      package com.sample;

       

      public class Example1 {
          public static void main(String[] args) throws Exception {
              new RuleRunner().runRules( new String[] { "Example1.drl" },
                                         new Object[0] );
          }
      }

       

      The value of String ruleFile is correctly printed but I get the following exception :

       

       

      Loading file: Example1.drl
      Exception in thread "main" java.lang.NullPointerException
          at java.io.Reader.<init>(Reader.java:78)
          at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
          at com.sample.RuleRunner.runRules(RuleRunner.java:26)
          at com.sample.Example1.main(Example1.java:5)

       

      What could be the reason?? Thanks