我有一个方法:
private String getProperty(String property) throws IOException,ConfigException {
// first test if the config file exists.
String propertyFile = "DataTransfer/Config/DataTransfer.properties";
if(!new File(propertyFile).exists()) {
throw new ConfigException("the config file doesn't exist." +
" Make sure the file : \""+propertyFile+"\" exists.");
}
// retrieve the property
Properties configFile = new Properties();
configFile.load(this.getClass().getClassLoader()
.getResourceAsStream(propertyFile));
String prop = configFile.getProperty(property);
return prop;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我一直在java.lang.NullPointerException
达到这个ConfigFile.load()
水平.
我在调试模式下检查了我的变量,它们都不是null.
我不知道这个例外的原因是什么.
您已检查它是否作为当前工作目录中的文件存在.您尚未检查它是否存在于类路径中.
如果您知道它作为文件存在,为什么不将其作为文件加载?
如果要从类路径加载它,为什么不更改检查以确保它存在于类路径中?
基本上,您需要在检查和装载之间保持一致.
归档时间: |
|
查看次数: |
4188 次 |
最近记录: |