Question

I cannot get rid of error:

  HTTP Status 500 - Request processing failed; nested exception is 
org.springframework.beans.BeanInstantiationException: 
Could not instantiate bean class [[Lmain.java.com.springapp.mvc.model.DSLR;]: 
No default constructor found; 
nested exception is java.lang.NoSuchMethodException: [Lmain.java.com.springapp.mvc.model.DSLR;.<init>()

I actually have constructor in DSLR class. What's wrong with my code?


Code here > Spring MVC WebApp: http://goo.gl/ddhLg5


DSLR class which is supposedly causing error:

package main.java.com.springapp.mvc.model;

import org.springframework.beans.factory.annotation.Autowired;

    public class DSLR {


    public DSLR() {
    }


    private int dslrId;
    private String model;
    private int price;
    private String description;

    public int getDslrId() {
        return dslrId;
    }

    public void setDslrId(int dslrId) {
        this.dslrId = dslrId;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "DSLR [dslr=" + dslrId + ", model=" + model
                + ", price=" + price+ ", description=" + description+"]";
    }
}

In DSLRServletController which instantiates DSLR I made changes:

@ModelAttribute("dslrs") DSLR dslrs[]

changed to:

@ModelAttribute("dslrs") List dslrs

which got rid of previous error and gave:

org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.util.List]: Specified class is an interface


SOLVED here: Spring MVC web application: No default constructor found If anyone can summarize and write the answer here I'd be happy to accept!

Était-ce utile?

La solution

In order for certain frameworks to initialize objects in such a way, you have to provide a default constructor (a constructor that takes no arguments), even if it doesn't do anything.

This is due to the fact you probably have another constructor in there that takes at least one argument. Logically, the library doesn't know what arguments to pass to every arbitrary class you pass it.

This is indicated in the error where it says java.lang.NoSuchMethodException: [Lmain.java.com.springapp.mvc.model.DSLR;.<init>():

  • NoSuchMethodException means it can't find a method it expects at runtime (through reflection)
  • .<init>() refers to the constructor (constructors don't technically have names, since they're always just the name of the class itself; therefore, the JVM refers to them as <init>()).

Autres conseils

As the error is impossible with the DSLR class you shared with us, my guess is that your server might contain an old class.

I suggest cleaning the project as well as the Tomcat installation from within your IDE.

  • Eclipse: Project -> Clean ...
  • Tomcat: Right click on the server in Servers view -> Clean ...
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top