Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoSuchMethodException: userAuth.User.<init>()

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?

like image 752
Lesya Makhova Avatar asked Aug 02 '13 18:08

Lesya Makhova


2 Answers

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:

public class User {      public User() {      }      .. } 
like image 107
micha Avatar answered Oct 07 '22 10:10

micha


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.

like image 27
zovits Avatar answered Oct 07 '22 09:10

zovits