Link to home
Start Free TrialLog in
Avatar of alzzz
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.getResourceAsStream( listOfFiles[i].toString() ) ) ); '

with a null pointer exception thrown.

the file exists and is' C:\myworkspace\settlement_engine\rules\TENNIS\2001.drl'

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;}
	}
	
	
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alzzz
alzzz

ASKER

not as of yet - thanking you
You might try:
Rule_Loader.class.getResourceAsStream( listOfFiles[i].getName()

Open in new window

Avatar of alzzz

ASKER

thanks
For your iteration, you'd be better with
builder.addPackageFromDrl(new InputStreamReader(new FileInputStream(listOfFiles[i]))); 

Open in new window

Avatar of alzzz

ASKER

lovely, just what i needed, much appreciated