Я пытаюсь прочитать содержимое файла yaml, используя java, и не могу создать объект. Вот файл yaml StudentLast:
year: 12
name: fewf
group: ewf
sex: ef
Вот код читателя:
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:
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;
}
}
Но работает читатель выдает следующее исключение:
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
Почему я получаю вышеуказанную ошибку и как ее исправить?