尝试实例化 Datepicker 时出现 Nullpointerexception (com.vaadin.flow.component.datepicker.DatePicker)

Nullpointerexception when trying to instantiate Datepicker (com.vaadin.flow.component.datepicker.DatePicker)

这有点奇怪,但是当我尝试实例化 DatePicker 组件时,我直接 Nullpointerexception :

DatePicker date = new DatePicker("Select date", LocalDate.now());

就这样,我输入了行(我已经在构造函数中尝试了不同的参数,并且还尝试了没有任何内容的默认构造函数),但由于 Nullpointer,我的应用程序没有启动...

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lectureForm' defined in file [D:\IDEAProjects\university\target\classes\com\university\ui\views\forms\LectureForm.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.university.ui.views.forms.LectureForm]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:313)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:294)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1358)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1204)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=12=](AbstractBeanFactory.java:323)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:893)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
    at com.university.Main.main(Main.java:13)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.university.ui.views.forms.LectureForm]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:217)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:309)
    ... 25 common frames omitted
Caused by: java.lang.NullPointerException: null
    at com.vaadin.flow.component.datepicker.DatePicker.<init>(DatePicker.java:85)
    at com.university.ui.views.forms.LectureForm.configureFormComponents(LectureForm.java:126)
    at com.university.ui.views.forms.LectureForm.<init>(LectureForm.java:68)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:204)
    ... 27 common frames omitted

DatePicker 中的第 85 行(并且始终是那一行)是

this.setLabel(label);

 public DatePicker(String label, LocalDate initialDate, ValueChangeListener<ComponentValueChangeEvent<DatePicker, LocalDate>> listener) {
        this(initialDate);
        this.setLabel(label);
        this.addValueChangeListener(listener);
    }

所以我不知道为什么会这样,为什么它总是相同的构造函数(不管参数如何),为什么问题出在标签上,最重要的是,如何解决它(- :

非常感谢您

问题的根源出乎意料 Spring 引导,我曾经 运行 应用程序。 在 IDE (IntelliJ IDEA) 行中,顺序不正确,但标有链接。所以 Nullpointerexception 确实不是在标签设置时抛出,而是在调用方法 UI.getCurrent() 时抛出,并且 DataPicker 试图设置语言环境。由于 Spring (@SpringComponent),方法调用到达时 Vaadin UI 尚未构建,因此 return 为 null。解决方案:将 @UIScope 注释添加到 class.