# Vaadin: Datepicker



## lusitano (10. Jul 2020)

Moin
Ich versuche mich gerade an Vaadin und wollte damit eine Form erstellen.
Sobald ich in die meine Form Klasse einen Datepicker 
	
	
	
	





```
DatePicker datePicker = new DatePicker();
```
erstelle bekomme ich eine Exception.
Erstelle ich den Datepicker in der Main bekomme ich aber keine Exception.

Was übersehe ich hier ?  
Grüße und danke.

Form Klasse 

```
import com.vaadin.flow.component.combobox.ComboBox;
import com.vaadin.flow.component.datepicker.DatePicker;
import com.vaadin.flow.component.formlayout.FormLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.component.timepicker.TimePicker;
import de.laprison.vaadin.uicomponents.fields.ComboBoxes;
import de.laprison.vaadin.uicomponents.fields.DateTimePickers;
import de.laprison.vaadin.uicomponents.fields.TextFields;
import org.springframework.stereotype.Service;



@Service
public class ReferenceForm extends FormLayout {

    private final ComboBoxes comboBoxes;
    private final TextFields textFields;
    private final DateTimePickers dateTimePickers;

    ComboBox<String> cobo = new ComboBox<>();
    TextField tfChargeNr = new TextField();
    TimePicker timePicker = new TimePicker();
    DatePicker datePicker = new DatePicker();

    public ReferenceForm() {

        addClassName("reference-view");
        setSizeFull();
        comboBoxes = new ComboBoxes();
        textFields = new TextFields();
        dateTimePickers = new DateTimePickers();

        add(
                configCobo(),
                configChargeNr(),
                configTimePicker()
        );
    }
```
Exception


```
2020-07-10 09:20:47.650  INFO 6032 --- [  restartedMain] dev-webpack                              : Started webpack-dev-server. Time: 397487874ms
Vaadin application has been deployed and started to the context path "/".
2020-07-10 09:20:47.743  WARN 6032 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'referenceForm' defined in file [D:\intellij-projects\testProjects\vaadin-testui\target\classes\de\laprison\vaadin\reference\ReferenceForm.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [de.laprison.vaadin.reference.ReferenceForm]: Constructor threw exception; nested exception is java.lang.NullPointerException
2020-07-10 09:20:47.746  INFO 6032 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-07-10 09:20:47.751  WARN 6032 --- [  restartedMain] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] appears to have started a thread named [Thread-4] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.base@12.0.1/java.net.PlainSocketImpl.accept0(Native Method)
 java.base@12.0.1/java.net.PlainSocketImpl.socketAccept(PlainSocketImpl.java:159)
 java.base@12.0.1/java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:458)
 java.base@12.0.1/java.net.ServerSocket.implAccept(ServerSocket.java:556)
 java.base@12.0.1/java.net.ServerSocket.accept(ServerSocket.java:524)
 app//com.vaadin.flow.server.DevServerWatchDog$WatchDogServer.run(DevServerWatchDog.java:58)
 java.base@12.0.1/java.lang.Thread.run(Thread.java:835)
2020-07-10 09:20:47.751  WARN 6032 --- [  restartedMain] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] appears to have started a thread named [webpack] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.base@12.0.1/java.io.FileInputStream.readBytes(Native Method)
 java.base@12.0.1/java.io.FileInputStream.read(FileInputStream.java:273)
 java.base@12.0.1/java.io.BufferedInputStream.read1(BufferedInputStream.java:290)
 java.base@12.0.1/java.io.BufferedInputStream.read(BufferedInputStream.java:351)
 java.base@12.0.1/sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
 java.base@12.0.1/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
 java.base@12.0.1/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
 java.base@12.0.1/java.io.InputStreamReader.read(InputStreamReader.java:185)
 java.base@12.0.1/java.io.BufferedReader.fill(BufferedReader.java:161)
 java.base@12.0.1/java.io.BufferedReader.readLine(BufferedReader.java:326)
 java.base@12.0.1/java.io.BufferedReader.readLine(BufferedReader.java:392)
 app//com.vaadin.flow.server.DevModeHandler.readLinesLoop(DevModeHandler.java:488)
 app//com.vaadin.flow.server.DevModeHandler.lambda$logStream$2(DevModeHandler.java:456)
 app//com.vaadin.flow.server.DevModeHandler$$Lambda$650/0x00000008015b4840.run(Unknown Source)
 java.base@12.0.1/java.lang.Thread.run(Thread.java:835)
2020-07-10 09:20:47.758  INFO 6032 --- [  restartedMain] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-07-10 09:20:47.767 ERROR 6032 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'referenceForm' defined in file [D:\intellij-projects\testProjects\vaadin-testui\target\classes\de\laprison\vaadin\reference\ReferenceForm.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [de.laprison.vaadin.reference.ReferenceForm]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1320) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at de.laprison.vaadin.Application.main(Application.java:14) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.2.0.RELEASE.jar:2.2.0.RELEASE]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [de.laprison.vaadin.reference.ReferenceForm]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:213) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1312) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    ... 22 common frames omitted
Caused by: java.lang.NullPointerException: null
    at com.vaadin.flow.component.datepicker.DatePicker.<init>(DatePicker.java:89) ~[vaadin-date-picker-flow-2.1.0.jar:na]
    at com.vaadin.flow.component.datepicker.DatePicker.<init>(DatePicker.java:76) ~[vaadin-date-picker-flow-2.1.0.jar:na]
    at de.laprison.vaadin.reference.ReferenceForm.<init>(ReferenceForm.java:26) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500) ~[na:na]
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481) ~[na:na]
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:200) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE]
    ... 24 common frames omitted
```


----------



## Flown (10. Jul 2020)

Du bekommst eine NPE weil Spring Beans nicht via Konstruktor erstellt. Du bräuchstest eine @PostConstruct Methode oder implementierst das InitializeBean Interface und da stopfst du deinen init-code rein. @see https://howtodoinjava.com/spring-core/spring-bean-life-cycle/


----------



## lusitano (10. Jul 2020)

Danke. Das werde ich mir anschauen.


----------



## mrBrown (10. Jul 2020)

Problem könnte dieses sein: https://github.com/vaadin/vaadin-date-picker-flow/issues/262



Flown hat gesagt.:


> Du bekommst eine NPE weil Spring Beans nicht via Konstruktor erstellt


Doch, eigentlich immer, in dem Fall über Constructor#newInstance.


----------



## Flown (10. Jul 2020)

mrBrown hat gesagt.:


> Doch, eigentlich immer, in dem Fall über Constructor#newInstance.


Du hast recht, natürlich wird ein Konstruktor aufgerufen, die Frage ist, zur richtigen Zeit und ich glaube nicht, dass das hier der Fall ist.


----------



## lusitano (11. Jul 2020)

Moin.

Ich habe eine Testprojekt erstellt und eine init() Methode mit @PostConstruct erstellt (der Timepicker funktioniert auch ohne init()) 
Das funktioniert jetzt auch wie gewollt. Danke hier noch mal an @Flown 
Nun aber meine Frage.
Wieso ? 
Also ich meine wieso muss der Datepicker in die init() @PostConstruct Methode ? 



```
import com.vaadin.flow.component.datepicker.DatePicker;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.timepicker.TimePicker;

import javax.annotation.PostConstruct;
import java.time.LocalDate;
import java.time.LocalTime;

public class DatePickers extends VerticalLayout {

    TimePicker timePicker = new TimePicker();

    public DatePickers() {

       add(configTimePicker(), configDatePicker());
    }

    @PostConstruct
    public void init(){
        configTimePicker();
        configDatePicker();
    }

    private TimePicker configTimePicker() {
        timePicker.setLabel("YOLO");
        timePicker.setValue(LocalTime.now());
        return timePicker;
    }

    private DatePicker configDatePicker() {
        DatePicker datePicker = new DatePicker();
        datePicker.setLabel("Date");
        datePicker.setValue(LocalDate.now());
        return datePicker;
    }

}
```


----------



## lusitano (11. Jul 2020)

Update:
Ich habe an dem funktionierendem Projekt etwas weiter gebastelt nur um zu schauen wie was funktioniert /oder auch nicht funktioniert).
Heraus kam das.

Main


```
import com.vaadin.flow.component.datepicker.DatePicker;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
import io.vaadin.test.testpackage.DatePickers;

@Route("")
public class MainView extends VerticalLayout {

    private final DatePickers datePickers;


    public MainView() {
        DatePicker datePicker = new DatePicker();
        addClassName("centered-content");

        datePickers = new DatePickers();

        DatePicker datePicker1 = datePickers.configDatePicker();

        add(datePicker, datePicker1);
    }

}
```
DatePickers

```
import com.vaadin.flow.component.datepicker.DatePicker;

import java.time.LocalDate;

public class DatePickers {

    public DatePicker configDatePicker() {
        DatePicker datePicker = new DatePicker();
        datePicker.setLabel("Datumsanzeige");
        datePicker.setValue(LocalDate.now());
        return datePicker;
    }

}
```

Jetzt frage ich mich natürlich warum das hier funktioniert.

1. Müsste nicht die Klasse Datepickers mit einer @Service o.Ä. Annotation versehen sein ?
2. Wieso funktioniert das hier jetzt ohne @PostConstruct init() ? 

Wäre super wen jemand noch mal drüber schauen könnte. 
Danke schon mal.


----------



## mrBrown (11. Jul 2020)

Wenn du die Klasse mit `@Service` annontierst, erstellt Spring direkt beim Start eine Instanz, die du dann injecten lassen kannst.

Du erstellst die Instanz aber selbst mit new selbst, die Service-Annotation ist also egal und die von Spring erstellte Instanz wird nicht genutzt.



Der Fehler zu Anfang war die "zu frühe" Erstellung der Instanz, offensichtlich war Vaadin dabei noch nicht passend initialisiert. Mit `PostConstruct` verschiebst du das ganze nach hinten, deshalb hatte es geholfen.


----------



## lusitano (12. Jul 2020)

Danke @mrBrown für die Erklärung.


----------

