代码之家  ›  专栏  ›  技术社区  ›  gshepherd7

使用SnAKEKAML的YAML到Java

  •  1
  • gshepherd7  · 技术社区  · 6 年前

    当我试图打印出对象时,无法理解为什么在我的配置类中的平台上得到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,它会很好地工作,尽管没有那些键值对。我错过了什么?

    1 回复  |  直到 6 年前
        1
  •  0
  •   efex09    6 年前

    更改字段的getter和setter方法 HW变量 '类的 站台 跟随。

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