0

I have a YAML File with following data and i want to Deserialize that into java Object.

connector:
  provider: ABCD
  nodes: 
  -  1.1.2.1
  -  1.2.2.8
  maxconnection: 1
  minconnection: 1

1.First i created a class called Connector where i have defined all the variable like Provider, Nodes, maxconnections, Minconnections. 2.Then i created a class called group and call the Connector class. 3.then in the main function tried to load the yaml file and call it.

But i got Null pointer exception Error

#Connector Class
public static class connector {

            private String provider;

            private String[]  nodes;

            private int max_connections;

            private int min_connections; 

            public String getprovider() {
                return provider;
            }
            public void setprovider(String provider) {
                this.provider = provider;
            }
           public String[] getnodes() {
              return nodes;
            }
            public void setnodes( String[] nodes) {
              this.nodes = nodes;
            }            
            public int getmax_connections() {
              return max_connections;
            }
            public void setmax_connections(int max_connections) {
              this.max_connections = max_connections;
            }
            public int getmin_connections() {
              return min_connections;
            }
            public void setmin_connections(int min_connections) {
              this.min_connections = min_connections;
            }

            @Override
            public String toString() {
                return "connector: {provider: " + this.provider + ", nodes: " + this.nodes + ",max_connections: " + this.max_connections +",min_connections: " + this.min_connections +"}";
            }


#Group class

        public static class Group {
            private ArrayList<connector> connector;

            public ArrayList<connector> getconnector() {
                return connector;
            }           

            public void setconnector(ArrayList<connector> connector) {
                this.connector = connector;
            }
        }

#main Class

public static void main(String[] args) {

            final URL resource = YamlConf.class.getResource("demo.yaml");
            final Constructor peopleContructor = new Constructor(Group.class);
            final TypeDescription peopleDescription = new TypeDescription(connector.class);
            peopleDescription.putMapPropertyType("connector", connector.class, Object.class);
            peopleContructor.addTypeDescription(peopleDescription);

            final Yaml yaml = new Yaml(peopleContructor);
            try {
                final Group group = (Group) yaml.load(resource.openStream());
                for (final connector connector : group.getconnector()) {
                    System.out.println(connector);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
Venkatachalam
  • 16,288
  • 9
  • 49
  • 77
Ranjan
  • 1
  • 1
  • 1
  • Please read [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it/24100776#24100776). – Sascha May 23 '19 at 12:31

1 Answers1

1

As you are using Jackson, you could use ObjectMapper with the YAMLFactory. Under the hood, it will use the SnakeYAML library. For the required dependencies, refer to the jackson-dataformats-text project documentation.

You could define your classes as follows:

@Getter
@Setter
@NoArgsConstructor
public class Content {

    private Connector connector;
}
@Getter
@Setter
@NoArgsConstructor
public class Connector {

    private String provider;

    private List<String> nodes;

    @JsonProperty("maxconnection")
    private Integer maxConnections;

    @JsonProperty("minconnection")
    private Integer minConnections;
}

The @Getter, @Setter and @NoArgsConstructor annotations are from the Lombok project. They will generate getters, setters and the default constructor for your. If you don't want to use Lombok, simply implement those methods.

Then read your YAML file:

ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
Content content = mapper.readValue(yamlFile, Content.class);

Depending on your needs, you could consider a Map<String, Object> to hold the values:

ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
TypeReference<Map<String, Object>> type = new TypeReference<Map<String, Object>>() {};
Map<String, Object> content = mapper.readValue(yamlFile, type);
cassiomolin
  • 124,154
  • 35
  • 280
  • 359