英文:
Nullpointerexception when trying to instantiate Datepicker (com.vaadin.flow.component.datepicker.DatePicker)
问题
当我尝试实例化DatePicker组件时,我会直接收到Nullpointerexception
,这有点奇怪:
DatePicker date = new DatePicker("选择日期", LocalDate.now());
就像这样,我输入这一行(我已经尝试过不同的构造函数参数以及没有任何参数的默认构造函数),但我的应用程序由于Nullpointer而无法启动...
DatePicker的第85行(总是那一行)是:
this.setLabel(label);
在以下构造函数中:
public DatePicker(String label, LocalDate initialDate, ValueChangeListener<ComponentValueChangeEvent<DatePicker, LocalDate>> listener) {
this(initialDate);
this.setLabel(label);
this.addValueChangeListener(listener);
}
所以我不知道为什么会发生这种情况,为什么总是相同的构造函数(无论参数如何),为什么问题出现在标签上,最重要的是,如何修复它(-:
非常感谢您提前的帮助。
英文:
It is a bit odd, but I get straight up Nullpointerexception
when I try to instantiate DatePicker component :
DatePicker date = new DatePicker("Select date", LocalDate.now());
Just like that, I enter the line (I already tried different parameters in the constructor and also default constructor with nothing in it), and my application does not launch due to 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
Line 85 (and it is always that line) in DatePicker is
this.setLabel(label);
in
public DatePicker(String label, LocalDate initialDate, ValueChangeListener<ComponentValueChangeEvent<DatePicker, LocalDate>> listener) {
this(initialDate);
this.setLabel(label);
this.addValueChangeListener(listener);
}
And so I don't have any ideas why this is happening, why it is always the same constructor (regardless of parameters), why the problem is with the label and, most importantly, how to fix it (-:
Thank you very much in advance
答案1
得分: 3
问题的根源出人意料地是 Spring Boot,我用它来运行应用程序。
在IDE(IntelliJ IDEA)中,行的顺序不正确,但带有链接。因此,Nullpointerexception
实际上不是在设置标签时抛出的,而是在调用 UI.getCurrent()
方法时抛出的,并且 DataPicker
尝试设置区域设置。由于使用了 Spring(@SpringComponent
),当方法调用到达时,Vaadin UI 还没有构建完成,因此返回值是 null
。解决方案:在类上添加 @UIScope
注解。
英文:
The source of the problem was unexpectably Spring Boot, that I used to run application.
In IDE (IntelliJ IDEA) lines where in incorrect order, but marked with links. So Nullpointerexception
was really thrown not at the label setting, but when method UI.getCurrent()
was called, and DataPicker
tried to set locale. Due to Spring (@SpringComponent
), Vaadin UI was not yet built when method call arrived, and so the return was null
. Solution : add @UIScope
annotation to the class.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论