Rifdar
Rifdar

Reputation: 23

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

I'm trying to read yaml file contents using java and am not able to construct an object. Here is the StudentLast yaml file:

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

Here is the reader code:

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

Here is the 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;
    }
}

But running the reader throws the following exception:

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

Why am I getting the above error and how can I fix it?

Upvotes: 2

Views: 6074

Answers (1)

Sarma
Sarma

Reputation: 440

Get rid of the 4-argument constructor. Or.. alternatively, add the default constructor.

That is your primary problem.


Also.. Try adding one single LINE at the TOP of your YAML file.

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

Upvotes: 2

Related Questions