【发布时间】:2020-08-27 12:24:11
【问题描述】:
这有点奇怪,但当我尝试实例化 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$0(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);
}
所以我不知道为什么会发生这种情况,为什么它总是相同的构造函数(不管参数如何),为什么问题出在标签上,最重要的是,如何解决它(-:
非常感谢您
【问题讨论】:
-
你的 Vaadin 版本是多少?
-
@BenjaminD 我使用 vaadin 16
-
你试过只有标签的构造函数吗?
-
@BenjaminD 是的,也没有用(
-
我找到了问题的根源——
UI.getCurrent()返回 null,并且因为在DataPicker对象中使用了此方法——Nullpointerexception被解决了。但我仍然不知道为什么会这样。
标签: java nullpointerexception datepicker vaadin vaadin-flow