Search code examples
mysqlspring-bootspring-mvcvaadinjavabeans

org.springframework.beans.factory.BeanCreationException: Error creating bean with name '': Bean instantiation via constructor failed;


i'm trying using mysql,spring and vaadin. the app should access to the db and show the table on the index page

in the code i have the interface repository extending jparepository, the service :

public class UtenteService {
    private UtenteRepository utenterep;
    
    public UtenteService(UtenteRepository utenterepository){
        this.utenterep=utenterepository;
    }
    public List<Utente> findAll(){
        return this.utenterep.findAll();
    }
    public void  save(Utente u){
        if(u==null){
            return;
        }
        utenterep.save(u);
    }
}

and the view where i call the service :

public class HelloView extends HorizontalLayout implements AfterNavigationObserver {
    
    @Autowired
    private UtenteService utenteService;
    
    private TextField name;
    private Button sayHello;
    private Grid<Utente> utenti = new Grid<>(Utente.class);
    private Binder<Utente> binder;

    


    public HelloView(UtenteService utenteService1) {
        this.utenteService=utenteService1;
        HorizontalLayout hor = new HorizontalLayout();
        setId("hello-view");
        name = new TextField("Your name");
        sayHello = new Button("Say hello");
        setVerticalComponentAlignment(Alignment.END, name, sayHello);
        sayHello.addClickListener( e-> {
            Notification.show("Hello " + name.getValue());
        });
        hor.add(name, sayHello);
        add(hor);
        configureGrid();
        hor.add(utenti);
        updategrid();
    }



    private void updategrid() {
        // TODO Auto-generated method stub
        utenti.setItems(utenteService.findAll());
    }



    private void configureGrid() {
        utenti.setSizeFull();
        utenti.setColumns("id","nome","password","id_ruolo");
        
        binder = new Binder<>(Utente.class);
        binder.bindInstanceFields(this);
        binder.setBean(new Utente());
        
        
    }

    @Override
    public void afterNavigation(AfterNavigationEvent event) {
        // TODO Auto-generated method stub
        utenti.setItems(utenteService.findAll());
        
    }  

ERROR

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'it.tirocinio.application.views.hello.HelloView': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [it.tirocinio.application.views.hello.HelloView]: Constructor threw exception; nested exception is java.lang.IllegalStateException: There are no instance fields found for automatic binding


Solution

  • Usually the last error is the root cause, there should be a stack trace that shows where the error occurred.

    Looking at the last error in the text you posted, you see java.lang.IllegalStateException: There are no instance fields found for automatic binding.

    So the error is likely because you are calling bindInstanceFields(this), but there are no matching fields. Your entity Utente has a field nome, but in HelloView it's called name.