当我试图打印出对象时,无法理解为什么在我的配置类中的平台上得到NullPointerException。
这是yaml文件:
platform:
platformType: CVN
hwVariant:
canesVariant: HW
hullNumber: XXX
tri: INST
这是我的父对象类configuration.java
public class Configuration {
private Platform platform;
public Configuration() {
}
public Platform getPlatform() {
return platform;
}
public void setPlatform(Platform platform) {
this.platform = platform;
}
@Override
public String toString() {
return platform.toString();
}
}
以下是my platform.java:
import java.util.Map;
public class Platform {
private String platformType;
private Map<String, String> hwVariant;
private String hullNumber;
private String tri;
public Platform() {
}
public String getPlatformType() {
return platformType;
}
public Map<String, String> getHWVariant() {
return hwVariant;
}
public String getHull() {
return hullNumber;
}
public String getTri() {
return tri;
}
public void setPlatformType(String platformType) {
this.platformType = platformType;
}
public void setHWVariant(Map<String, String> hwVariant) {
this.hwVariant = hwVariant;
}
public void setHullNumber(String hullNumber) {
this.hullNumber = hullNumber;
}
public void setTri(String tri) {
this.tri = tri;
}
@Override
public String toString() {
return "platform: \n" +
" platformType: " + platformType + "\n" +
" hwVariant: " + hwVariant + "\n" +
" hullNumber: " + hullNumber + "\n" +
" tri: " + tri;
}
}
以下是我得到的nullpointerException:
File is: E:\workspace_neon\PlatformInterviewer\resources\uswdss-platform.yml
Exception in thread "main" Can't construct a java object for tag:yaml.org,2002:net.progeny.setup.model.Configuration; exception=null
in 'reader', line 1, column 1:
platform:
^
at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:314)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:204)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:193)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:159)
at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:146)
at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:524)
at org.yaml.snakeyaml.Yaml.loadAs(Yaml.java:518)
at net.progeny.setup.YamlConfiguration.main(YamlConfiguration.java:28)
Caused by: java.lang.NullPointerException
at net.progeny.setup.model.Configuration.toString(Configuration.java:31)
at java.lang.String.valueOf(String.java:2994)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:268)
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:149)
at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:309)
... 7 more
当我添加了内部嵌套的hwvariant选项时,似乎无法使其正常工作。如果我从yaml文件中删除hwvariant和canesvariant,它会很好地工作,尽管没有那些键值对。我错过了什么?