alzzz
asked on
java null pointer exception when loading a resource
Hi,
the attached code fails on the line
'builder.addPackageFromDrl (new InputStreamReader( Rule_Loader.class.getResou rceAsStrea m( listOfFiles[i].toString() ) ) ); '
with a null pointer exception thrown.
the file exists and is' C:\myworkspace\settlement_ engine\rul es\TENNIS\ 2001.drl'
Can anybody see whatim doing wrong?
thanks in advance
Alex
the attached code fails on the line
'builder.addPackageFromDrl
with a null pointer exception thrown.
the file exists and is' C:\myworkspace\settlement_
Can anybody see whatim doing wrong?
thanks in advance
Alex
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.compiler.DroolsParserException;
import org.drools.compiler.PackageBuilder;
import org.drools.rule.Package;
import com.paddypower.ramp.settlement_engine.misc.Constants;
/**
* loads all rules for the specified sport type
*
* @author
*
*/
public class Rule_Loader {
public RuleBase load_rules(String sport_type){
RuleBase rulebase = null;
try{
RuleBase ruleBase = RuleBaseFactory.newRuleBase();
PackageBuilder builder = new PackageBuilder();
File folder = new File(Constants.RULES_FILE + "/" + sport_type);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile())
builder.addPackageFromDrl(new InputStreamReader( Rule_Loader.class.getResourceAsStream( listOfFiles[i].toString() ) ) );
}
Package pkg = builder.getPackage();
ruleBase.addPackage( pkg );
return rulebase;
}
catch(IOException ioe){System.out.println(ioe.toString());return rulebase = null;}
catch(DroolsParserException dpe){System.out.println(dpe.toString());return rulebase = null;}
catch(Exception e){System.out.println(e.toString());
return rulebase = null;}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You might try:
Rule_Loader.class.getResourceAsStream( listOfFiles[i].getName()
ASKER
thanks
For your iteration, you'd be better with
builder.addPackageFromDrl(new InputStreamReader(new FileInputStream(listOfFiles[i])));
ASKER
lovely, just what i needed, much appreciated
ASKER