gshepherd7
gshepherd7

Reputation: 145

YAML to Java using snakeyaml

Can't figure out why I am getting a NullPointerException on my platform in my Configuration class when I try to print out the object.

Here's the yaml file:

platform:
  platformType: CVN
  hwVariant:
    canesVariant: HW
  hullNumber: XXX
  tri: INST

Here is my parent object class 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();
}
}

Here's 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;
}

}

Here's the NullPointerException I'm getting:

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

I just can't seem to get it working when I added the inner nested hwVariant option. If I remove the hwVariant and canesVariant from my yaml file it works fine, albeit without those key value pairs. What am I missing?

Upvotes: 2

Views: 3342

Answers (1)

efex09
efex09

Reputation: 419

Change Getter and Setter method of field 'hwVariant' of class Platform to following.

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

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

SnakeYaml internally uses jackson to map yaml document into a java object. Jackson internally calls getter and setter method of the fields, so it expects getter and setter to be in standard form.

Upvotes: 1

Related Questions