我在 Java 中有一个始终为 null 的值,但我不明白为什么,因为我正在使用类构造函数设置变量的值。我有这样的代码:Driver driverClass = new Driver("<file path redacted>");然后是下面的:String cfgFilePath;public Driver(String cfgFile) { this.cfgFilePath = cfgFile;}private ArrayList<String> keys = getKeys(cfgFilePath);private String a1 = keys.get(0);private String b1 = keys.get(1);出于某种原因,IntelliJ IDEA 说 cfgFilePath 始终为空。我用 Driver 类构造函数初始化它,为什么它是空的?当我运行程序时,我得到一个空指针异常。
1 回答
慕桂英4014372
TA贡献1871条经验 获得超13个赞
将keys,a1和的初始化移动b1到您的构造函数中,例如:
public Driver(String cfgFile) {
this.cfgFilePath = cfgFile;
this.keys = getKeys(cfgFilePath);
this.a1 = keys.get(0);
this.b1 = keys.get(1);
}
private ArrayList<String> keys = new ArrayList<>();
private String a1;
private String b1;
添加回答
举报
0/150
提交
取消