无法为 tag:yaml.org、2002exception=java.lang.InstantiationException 构建 java 对象:

Can't construct a java object for tag:yaml.org,2002exception=java.lang.InstantiationException:

我正在尝试使用 java 读取 yaml 文件内容,但无法构建对象。这是 StudentLast yaml 文件:

year: 12 
name: fewf
group: ewf
sex: ef

这里是 reader 代码:

public class StudentYamlReader {
    public static void main(String[] args) throws FileNotFoundException {
        StudentYamlReader c = new StudentYamlReader();
        c.write();
    }

    public void write() throws FileNotFoundException {
        Yaml yaml = new Yaml(new Constructor(StudentLast.class));
        InputStream inputStream = getClass()
                .getClassLoader()
                .getResourceAsStream("StudentLast.yaml");
        StudentLast studentLast = yaml.loadAs(inputStream,StudentLast.class);
        System.out.println(studentLast.getSex());

       // System.out.println(obj);
    }
}

这是 StudentLast class:

public class StudentLast  {
    public int year;
    public String name;
    public String sex;
    public String group;

    public StudentLast(int year, String name, String sex, String group) {
        this.year = year;
        this.name = name;
        this.sex = sex;
        this.group = group;
    }
}

但是 运行 reader 抛出以下异常:

Exception in thread "main" Can't construct a java object for tag:yaml.org,2002:StudentLast; exception=java.lang.InstantiationException: NoSuchMethodException:StudentLast.<init>()
 in 'reader', line 1, column 1:
    year: 12
    ^

    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 StudentYamlReader.write(StudentYamlReader.java:27)
    at StudentYamlReader.main(StudentYamlReader.java:19)
Caused by: org.yaml.snakeyaml.error.YAMLException: java.lang.InstantiationException: NoSuchMethodException:StudentLast.<init>()
    at org.yaml.snakeyaml.constructor.BaseConstructor.newInstance(BaseConstructor.java:277)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:145)
    at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:309)
    ... 8 more
Caused by: java.lang.InstantiationException: NoSuchMethodException:StudentLast.<init>()
    at org.yaml.snakeyaml.constructor.BaseConstructor.newInstance(BaseConstructor.java:307)
    at org.yaml.snakeyaml.constructor.BaseConstructor.newInstance(BaseConstructor.java:282)
    at org.yaml.snakeyaml.constructor.BaseConstructor.newInstance(BaseConstructor.java:275)
    ... 10 more

Process finished with exit code 1

为什么会出现上述错误,我该如何解决?

摆脱 4 参数构造函数。 或者.. 或者,添加默认构造函数。

这是你的首要问题。


还有.. 尝试在您的 YAML 文件的 TOP 添加一行。

!!com.rifdar.StudentLast
year: 12 
name: fewf
group: ewf
sex: ef