java.lang.NoSuchMethodException: userAuth.User.<init>()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18023870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.NoSuchMethodException: userAuth.User.<init>()
提问by Lesya Makhova
I have the class with validation:
我有经过验证的课程:
public class User {
@Size(min=3, max=20, message="User name must be between 3 and 20 characters long")
@Pattern(regexp="^[a-zA-Z0-9]+$", message="User name must be alphanumeric with no spaces")
private String name;
@Size(min=6, max=20, message="Password must be between 6 and 20 characters long")
@Pattern(regexp="^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$", message="Password must contains at least one number")
private String password;
public User(String _name, String _password){
super();
name = _name;
password = _password;
}
public String getName(){
return name;
}
public String getPassword(){
return password;
}
public void setPassword(String newPassword){
password = newPassword;
}
}
when I validate values, I got the message:
当我验证值时,我收到消息:
SEVERE: Servlet.service() for servlet osAppServlet threw exception
java.lang.NoSuchMethodException: userAuth.User.<init>()
where is the problem?
问题出在哪儿?
采纳答案by micha
The message java.lang.NoSuchMethodException: userAuth.User.<init>()means that someone tried to call a constructor without any parameters. Adding a default constructor should solve this problem:
该消息java.lang.NoSuchMethodException: userAuth.User.<init>()意味着有人试图调用没有任何参数的构造函数。添加默认构造函数应该可以解决这个问题:
public class User {
public User() {
}
..
}
回答by seralex.vi
Add constructor without parameters:
添加不带参数的构造函数:
public class User {
...
public User() {}
...
}
回答by D. Lawrence
I think the answer of adding a default no-args constructor is clear. However, one must also think about the visibility of the latter constructor. The previous answers show the use of a publicno-args default constructor. Is the publicvisibility relevant with respect to the class' logic?
我认为添加默认无参数构造函数的答案很明确。但是,还必须考虑后一个构造函数的可见性。前面的答案显示了公共无参数默认构造函数的使用。是公众知名度有关关于类的逻辑是什么?
As a matter of fact, Hibernate doesn't require a publicno-args constructor. One can still choose its visibility.
事实上,Hibernate 不需要公共的无参数构造函数。人们仍然可以选择其可见性。
I would therefore recommend to take great care of the constructor's visibility (private/public/protected/default). Choose the adequate one. For example, if no one should be able to call this no args constructor, choose a privatevisibility like so:
因此,我建议非常注意构造函数的可见性(私有/公共/受保护/默认)。选择合适的。例如,如果没有人应该能够调用此无参数构造函数,请选择一个私有可见性,如下所示:
public class User {
...
private User() {}
...
}
回答by zovits
If the User class is a non-static inner class of another class (e.g. UserAuth), then the message could arise if the User class is attempted to be instantiated before the external class is instantiated. In this case the null-arg constructor might be there in the code but will not be found, since the external class does not exist yet. A solution could be to extract the inner class into an independent class, to make it static or to ensure the initialization of the external class happens before that of the inner class.
如果 User 类是另一个类(例如 UserAuth)的非静态内部类,则如果在实例化外部类之前尝试实例化 User 类,则可能会出现该消息。在这种情况下,null-arg 构造函数可能存在于代码中,但不会被找到,因为外部类尚不存在。一个解决方案可能是将内部类提取到一个独立的类中,使其成为静态或确保外部类的初始化发生在内部类的初始化之前。

