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

Khan

Getting this error while reading 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)

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;

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";
    }

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;
        }
    }
achabahe

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 controlle

    @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.

EDIT
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";
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

java.lang.NoSuchMethodException for onCreate

From Java

Spring bean java.lang.NoSuchMethodError error

From Dev

java.lang.NoSuchMethodException: Unknown property

From Dev

Java - java.lang.NoSuchMethodException

From Dev

No default constructor found; nested exception is java.lang.NoSuchMethodException with Spring MVC?

From Dev

java.lang.NoSuchMethodException: setHomeActionContentDescription [int]?

From Dev

ProGuard java.lang.NoSuchMethodException

From Dev

A java.lang.NoSuchMethodException error (layouts)

From Dev

Exception while starting up the server : java.lang.NoSuchMethodException: org.springframework.security.authentication.ProviderManager.<init>()

From Dev

Spring NumberFormatException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long

From Dev

Spring, Java : Error, cannot cast java.lang.Long to java.util.Date

From Dev

java.lang.RuntimeException: java.lang.NoSuchMethodException: Hadoop mapreduce

From Dev

Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]

From Dev

Java Spring MVC - java.lang.NoClassDefFoundError: javax/servlet/ServletContext

From Dev

getDeclaredMethod leading to java.lang.NoSuchMethodException

From Dev

Kafka throws java.lang.NoSuchMethodException

From Dev

android java.lang.AssertionError: java.lang.NoSuchMethodException - Proguard

From Dev

What causing this exception java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class android.view.View]

From Dev

Java reflection: constructor for primitive int causes: java.lang.NoSuchMethodException: int.<init>(int)

From Dev

java.lang.NoSuchMethodException for onCreate

From Dev

Spring bean java.lang.NoSuchMethodError error

From Dev

Java reflection: constructor for primitive int causes: java.lang.NoSuchMethodException: int.<init>(int)

From Dev

Spring Security Java NoSuchMethodException: SecurityConfig.<init>()

From Dev

A java.lang.NoSuchMethodException error (layouts)

From Dev

java.lang.String cannot be cast to java.lang.Long in Spring Security ACL

From Dev

java.lang.RuntimeException: java.lang.NoSuchMethodException: Hadoop mapreduce

From Dev

java.lang.NoSuchMethodException struts servlet

From Dev

java.lang.NoSuchMethodException: AffirmativeBased.<init>()

From Dev

java.lang.NoSuchMethodException: <Class>.<init>(java.lang.String) when copying custom Transformer

Related Related

  1. 1

    java.lang.NoSuchMethodException for onCreate

  2. 2

    Spring bean java.lang.NoSuchMethodError error

  3. 3

    java.lang.NoSuchMethodException: Unknown property

  4. 4

    Java - java.lang.NoSuchMethodException

  5. 5

    No default constructor found; nested exception is java.lang.NoSuchMethodException with Spring MVC?

  6. 6

    java.lang.NoSuchMethodException: setHomeActionContentDescription [int]?

  7. 7

    ProGuard java.lang.NoSuchMethodException

  8. 8

    A java.lang.NoSuchMethodException error (layouts)

  9. 9

    Exception while starting up the server : java.lang.NoSuchMethodException: org.springframework.security.authentication.ProviderManager.<init>()

  10. 10

    Spring NumberFormatException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long

  11. 11

    Spring, Java : Error, cannot cast java.lang.Long to java.util.Date

  12. 12

    java.lang.RuntimeException: java.lang.NoSuchMethodException: Hadoop mapreduce

  13. 13

    Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]

  14. 14

    Java Spring MVC - java.lang.NoClassDefFoundError: javax/servlet/ServletContext

  15. 15

    getDeclaredMethod leading to java.lang.NoSuchMethodException

  16. 16

    Kafka throws java.lang.NoSuchMethodException

  17. 17

    android java.lang.AssertionError: java.lang.NoSuchMethodException - Proguard

  18. 18

    What causing this exception java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class android.view.View]

  19. 19

    Java reflection: constructor for primitive int causes: java.lang.NoSuchMethodException: int.<init>(int)

  20. 20

    java.lang.NoSuchMethodException for onCreate

  21. 21

    Spring bean java.lang.NoSuchMethodError error

  22. 22

    Java reflection: constructor for primitive int causes: java.lang.NoSuchMethodException: int.<init>(int)

  23. 23

    Spring Security Java NoSuchMethodException: SecurityConfig.<init>()

  24. 24

    A java.lang.NoSuchMethodException error (layouts)

  25. 25

    java.lang.String cannot be cast to java.lang.Long in Spring Security ACL

  26. 26

    java.lang.RuntimeException: java.lang.NoSuchMethodException: Hadoop mapreduce

  27. 27

    java.lang.NoSuchMethodException struts servlet

  28. 28

    java.lang.NoSuchMethodException: AffirmativeBased.<init>()

  29. 29

    java.lang.NoSuchMethodException: <Class>.<init>(java.lang.String) when copying custom Transformer

HotTag

Archive