当前位置: 动力学知识库 > 问答 > 编程问答 >

class - File Yaml to object using Yamlbeans library

问题描述:

I tried using yaml file named contact to write it into object. Unfortunately it didn't work...

 package javaapplication22;

import com.esotericsoftware.yamlbeans.YamlException;

import com.esotericsoftware.yamlbeans.YamlReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

public class JavaApplication22 {

public class Contact {

public String name;

public int age;

}

public static void main(String[] args) throws FileNotFoundException, YamlException {

YamlReader reader = new YamlReader(new FileReader("contact.yml"));

Contact contact = reader.read(Contact.class);

System.out.println(contact.age);

}

}

I don't know why it doesn't work.Could somebody help me?

网友答案:

You can't reference JavaApplication22$Contact in a static-method.


In one word, in order to call

Contact contact = reader.read(Contact.class);

You have to make sure you can call the constructor there:

Contact test = new Contact();

In your code, you can't new an object of class Contact in main method.


There are two methods to work around:

1) change Contact to static

public static class Contact {
    public String name;
    public int age;
}

2) Don't define Contact as an inner class

分享给朋友:
您可能感兴趣的文章:
随机阅读: