使用snakeyaml将YAML转换为Java

时间:2018-07-18 21:38:12

标签: yaml snakeyaml

当我尝试打印对象时,无法弄清楚为什么我的平台上的Configuration类中会出现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();
}
}

这是我的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,即使没有这些键值对,它也能正常工作。我想念什么?

1 个答案:

答案 0 :(得分:0)

将类平台的字段' hwVariant '的Getter和Setter方法更改为以下内容。

public Map<String, String> getHwVariant() {
    return hwVariant;
}

public void setHwVariant(Map<String, String> hwVariant) {
    this.hwVariant = hwVariant;
}