我已经设置了一个私有静态属性对象加载在我的工厂类的一些配置工厂类的构造函数中。私人静态属性变量导致空指针异常
public class BussinessServiceFactory {
private static final BussinessServiceFactory factory = new BussinessServiceFactory();
public static BussinessServiceFactory createBussinessServiceFactory(){
return factory;
}
private BussinessServiceFactory(){
InputStream in = BussinessServiceFactory.class.getClassLoader().getResourceAsStream("factory/bussinessservice.properties");
try {
bsConfig.load(in);
} catch (IOException exception) {
// TODO Auto-generated catch block
throw new RuntimeException(exception);
}
}
private static Properties bsConfig = new Properties();
public <T> T createBussinessService(Class<T> clazz){
try {
String clazzName = clazz.getSimpleName();
String name = bsConfig.getProperty(clazzName);
return (T) Class.forName(name).newInstance();
} catch (InstantiationException | IllegalAccessException
| ClassNotFoundException exception) {
throw new RuntimeException(exception);
}
}
}
但是,当我初始化类时,它会抛出NullPointerException
。
java.lang.NullPointerException
at factory.BussinessServiceFactory.<init>(BussinessServiceFactory.java:15)
at factory.BussinessServiceFactory.<clinit>(BussinessServiceFactory.java:8)
... 24 more
然后,如果我将属性对象更改为非静态对象,问题就解决了。但我仍然对这种情况发生的原因以及如何以这种方式解决问题感到困惑。
那么,例外说这个问题是在* testFindOrder *,你向我们展示* testPageQuery *。我们如何帮助你? – CKing