Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: java.lang.NoSuchMethodException: java.lang.Long.<init>() in spring MVC

I am getting this error while reading a student object from database.

org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.lang.Long]: No default constructor found; nested exception is java.lang.NoSuchMethodException: java.lang.Long.<init>()
org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:107)

Full stack trace:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.lang.Long]: No default constructor found; nested exception is java.lang.NoSuchMethodException: java.lang.Long.<init>()
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause

org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.lang.Long]: No default constructor found; nested exception is java.lang.NoSuchMethodException: java.lang.Long.<init>()
    org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:107)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:775)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:368)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:172)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause

java.lang.NoSuchMethodException: java.lang.Long.<init>()
    java.lang.Class.getConstructor0(Class.java:3082)
    java.lang.Class.getDeclaredConstructor(Class.java:2178)
    org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:775)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:368)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:172)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

File Student.java

@Entity
@Table(name="Student")
public class Student implements Serializable{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="studentId")
    Long studentId;

    @Column(name="studentName")
    String studentName;

File Controller.java

    @RequestMapping(value = "/read.html")
    public String readStudent(Model model, @ModelAttribute("studentId") Long studentId) {
        Student student = null;
        studentId = 2l;
        try{
            student = serviceFile.readStudent(studentId);
        }catch(Exception e){
            model.addAttribute("message", "Some thing went wrong !!!! Exception occoured");
            return "message";
        }
        model.addAttribute("student", student);
        return "read";
    }

File daoImpl.java

@Repository
@Transactional
public class DaoImplFile implements DaoFile {

    private EntityManager entityManager;

    public EntityManager getEntityManager() {
        return entityManager;
    }

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Override
    public Student read(Long studentId) throws NullPointerException {
        Student student = entityManager.find(Student.class, studentId);
        if (student!=null) {
            return student;
        } else {
            return null;
        }
    }
like image 510
Noorus Khan Avatar asked Mar 04 '16 22:03

Noorus Khan


1 Answers

The @ModelAttribute("studentId") Long studentId is the source of the problem, because Spring doesn't find a method that can provide this Long object, so it tries to instantiate one and pass it as a method argument. To solve this problem you can either:

  • Delete @ModelAttribue from the method argument

     @RequestMapping(value = "/read.html")
     public String readStudent(Model model,Long studentId) {
         Student student = null;
         studentId = 2l;
         try {
             student = serviceFile.readStudent(studentId);
         } catch(Exception e){
             model.addAttribute("message", "Some thing went wrong !!!! Exception occured");
             return "message";
         }
         model.addAttribute("student", student);
         return "read";
     }
    
  • Create a method that will provide that Long Object in your controller

     @ModelAttribute
     public void provideStudentId(Model model){
         model.addAttribute("studentId", new Long(1));
     }
    

Official Doc

@RequestMapping(path = "/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@ModelAttribute Pet pet) { }

Given the above example where can the Pet instance come from? There are several options:

  1. It may already be in the model due to use of @SessionAttributes — see the section called “Using @SessionAttributes to store model attributes in the HTTP session between requests”.
  2. It may already be in the model due to an @ModelAttribute method in the same controller — as explained in the previous section.
  3. It may be retrieved based on a URI template variable and type converter (explained in more detail below).
  4. It may be instantiated using its default constructor.

If the studentId was the parameter name sent from the UI, you can use @RequestParam like this

@RequestMapping(value = "/read.html")
public String readStudent(Model model, @RequestParam("studentId") Long studentId) {
    Student student = null;
    studentId = 2l;
    try {
        student = serviceFile.readStudent(studentId);
    } catch(Exception e) {
        model.addAttribute("message", "Some thing went wrong !!!! Exception occoured");
        return "message";
    }
    model.addAttribute("student", student);
    return "read";
}
like image 106
achabahe Avatar answered Nov 03 '22 00:11

achabahe