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();
}
}