0

good morning everyone. I'm starting to use vaadin 10, I'm trying to set items in a grid with Spring Jpa as follows, but for me it marks an error, this situation does not happen in the previous versions of vaadin.

@HtmlImport("styles/shared-styles.html") @Route("") public class MainView extends VerticalLayout {

@Autowired
private UserService userService;

private Grid<User> grid = new Grid<>();

public MainView() { 

grid.setItems(userService.selectAll());

add(grid);

}

I got the following error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.portal.app.spring.MainView': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.portal.app.spring.MainView]: Constructor threw exception; nested exception is java.lang.NullPointerException

.....

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.portal.app.spring.MainView]: Constructor threw exception; nested exception is java.lang.NullPointerException at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:182) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1228) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE] ... 72 common frames omitted Caused by: java.lang.NullPointerException: null

from what I understand, this is caused by @autowire in my service.

I hope someone shares with me an idea of ​​how I resolve the error, thanks :)

codeLover
  • 2,571
  • 1
  • 11
  • 27
  • Spring can't possibly autowire a field of an object if the object doesn't exist yet. The object only exists after the constructor has executed. Ergo an autowired field can only be null inside the constructor. Stop using field injection, and use constructor injection instead. And use PostConstruct. – JB Nizet Jul 14 '18 at 06:56
  • There is super simple example project here: https://vaadin.com/start/v10-project-base-spring Use it as baseline to get started with Vaadin 10 + Spring. – Tatu Lund Jul 14 '18 at 07:40

0 Answers0