Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SnakeYaml error: No writable property on class

I've made a stupid mistake? I am new to yaml. Don't know why but my System does not see my variable called 'name'.

Full error

Error:

cannot create property=name for JavaBean=x.ModuleDescription@7ce6a65d

My class:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ModuleDescription {

    private String name;
    private String main;

    private String version = "unknown";
    private String author = "unknown";

    @Setter
    private File file = null;
}

I can see this field but my system does not. Hope that anyone got an idea. Thanks!


1 Answers

Well you declare the field name to be private which prohibits anyone else from accessing the field. By default, SnakeYAML only loads public fields and properties, i.e. values with public getters and setters.

So a simple fix would be to add a public getter and setter for name to the class:

public String getName() {
    return name;
}
public void setName(final String value) {
    name = value;
}

For loading, you need the setter; for dumping, you need the getter. Alternatively, you can declare the field public.

If you really really want to keep the field private and not add any public getters / setters, this could also work (untested):

Yaml yaml = new Yaml();
yaml.setBeanAccess(BeanAccess.FIELD);
ModuleDescription desc = yaml.loadAs(/* YAML source here */,
                                     ModuleDescription.class);
like image 144
flyx Avatar answered Feb 13 '26 17:02

flyx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!