有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

爪哇懒虫名单。构造函数:构造函数必须存在并且是公共异常

我有密码:

public class User   
...
private List<Country> countries = LazyList.decorate(new ArrayList(), FactoryUtils.instantiateFactory(Country.class));
    private String country;

...
public void setCountries(List<Country> countries) {
        this.countries = countries;
    }

    public List<Country> getCountries() {
        return countries;
    }
...

在国家级:

public class Country {

    private int countryId;
    private String countryName;

    public Country(int countryId, String countryName)
    {
        this.countryId = countryId;
        this.countryName = countryName;
    }

    public int getCountryId() {
        return countryId;
    }

    public void setCountryId(int countryId) {
        this.countryId = countryId;
    }

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

}

当我创建一个新的用户对象时,我给出了一个例外:

java.lang.IllegalArgumentException: InstantiateFactory: The constructor must exist and be public

有人知道为什么吗


共 (1) 个答案

  1. # 1 楼答案

    似乎你唯一的构造函数是:

    public Country(int countryId, String countryName)
    

    虽然工厂希望找到无arg构造函数(常见要求):

    public Country()
    

    把它加到你的Country课上,你就没事了