Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

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

0
alzzz
Asked:
alzzz
  • 3
  • 3
1 Solution
 
CEHJCommented:
You can't load files like that really, unless they happen to be in your classpath. Are they?
0
 
alzzzAuthor Commented:
not as of yet - thanking you
0
 
CEHJCommented:
You might try:
Rule_Loader.class.getResourceAsStream( listOfFiles[i].getName()

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Open in new window

0
 
alzzzAuthor Commented:
lovely, just what i needed, much appreciated
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now