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

92,282

Solution 1

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() {

    }

    ..
}

Solution 2

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.

Solution 3

Add constructor without parameters:

public class User {
  ...
  public User() {}
  ...
}

Solution 4

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 public no-args default constructor. Is the public visibility relevant with respect to the class' logic?

As a matter of fact, Hibernate doesn't require a public no-args constructor. One can still choose its visibility.

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 private visibility like so:

public class User {
  ...
  private User() {}
  ...
}
Share:
92,282
Lesya Makhova
Author by

Lesya Makhova

Java Developer Spring, Web, JPA, Hibernate

Updated on July 09, 2022

Comments

  • Lesya Makhova
    Lesya Makhova almost 2 years

    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?

  • Muthu Ganapathy Nathan
    Muthu Ganapathy Nathan almost 6 years
    Great! This is my exact problem!
  • Jeff
    Jeff almost 6 years
    I had to make my constructor public.
  • pkramaric
    pkramaric almost 6 years
    Had the same problem, once I took the non-static inner class out and made it a separate class it worked fine for me
  • deadLock
    deadLock over 3 years
    In my case, other access modes still cause the error. So had to go with the public mode only. Surprisingly, Intellij IDEA recommends me to remove no args constructor.
  • User1291
    User1291 over 3 years
    A similar issue, as noted by stackoverflow.com/a/27580043/3322533 , a private static class will have a non-public default constructor.