DateField and TextField issues

Windows 7 x64
Vaadin 8.4.1

Hi,

I have DateField component binded to LocalDate field “fecha” which is part of an Entity.

//in Entity
private LocalDate fecha;

Binder<Deposito> binder = new Binder<>(Deposito.class);
DateField txtFecha = new DateField("Fecha de entrada");
txtFecha.setValue(LocalDate.now());
binder.forField(txtFecha).bind(Deposito::getFecha, Deposito::setFecha); 

txtFecha once being part of Entity (JPA Hibernate) would not recive the init date value LocalDate.now()


  1. I’m getting a java.lang.NullPointerException
    at com.vaadin.data.Binder$BindingImpl.lambda$writeFieldValue$22abd369$1(Binder.java:1187)

    when the TextField has no data (empty).

    `
    // in the Entity
    @Column(name = “CANTIDAD_CAJONES”)
    private long numCajones;

    TextField txtNumCajas = new TextField(“Nº de Cajas”);

    binder.forField(txtNumCajas)
    .withNullRepresentation(“”)
    .withConverter(new StringToLongConverter(setMensajeError(“Nº de Cajas”, MensajeError.CAMPO_NUMERO)))
    .bind(Deposito::getNumCajones, Deposito::setNumCajones);
    `

    The field should have allowed value 0 and empty string (“”). So, how to capture and/or ignore this NPE.

  2. Problem with DateField
    At compiling the project (NetBeans - compile on Save) I recive the following error:

    ADVERTENCIA: No puedo serializar atributo de sesión [com.vaadin.server.VaadinSession.CapitanNemoStockUIServlet]

    para sesión [89AAAFDA15576EE5ACD18FAE8A4FD1CC]

    java.io.NotSerializableException: java.time.format.DateTimeFormatter

    Anyway, the date formating is OK.

    `
    //in Entity
    private LocalDate fecha;

    Grid table = new Grid();
    table.addColumn(Deposito::getFecha).setId(“fechaCol”).setCaption(“Fecha”).setWidth(85);
    table.getColumn(“fechaCol”).setRenderer((Renderer) new LocalDateRenderer(“dd/MM/yyyy”));
    `

    I’m doing something wrong?

Regards,
Aleksander
17070758.jpg
17070761.jpg

I repeat the code as formatted.
2)

// in the Entity 
@Column(name = "CANTIDAD_CAJONES") private long numCajones;

TextField txtNumCajas = new TextField("Nº de Cajas");

binder.forField(txtNumCajas) .withNullRepresentation("") .withConverter(new StringToLongConverter(setMensajeError("Nº de Cajas", MensajeError.CAMPO_NUMERO))) .bind(Deposito::getNumCajones, Deposito::setNumCajones); `
//in Entity 
private LocalDate fecha;

Grid table = new Grid(); table.addColumn(Deposito::getFecha).setId("fechaCol").setCaption("Fecha").setWidth(85); table.getColumn("fechaCol").setRenderer((Renderer) new LocalDateRenderer("dd/MM/yyyy")); `

hey Aleksander, sorry for Question but what Vaadin Version yre you using ?

Asking because of

Grid table = new Grid();
and
table.getColumn("fechaCol").setRenderer((Renderer) new LocalDateRenderer("dd/MM/yyyy"));

in V8 you can simply write

Grid<Deposito> grid = new Grid<>();
grid.addColumn(Deposito::getFecha)
	.setId("fechaCol")
	.setCaption("Fecha")
	.setWidth(85)
	.setRenderer(new LocalDateRenderer("dd/MM/yyyy"));

So before i can reproduce your Problem, i have to setup the right enviroment :slight_smile:

i reproduced your code in my demo Project and it works just fine.

Binder<Person> binder = new Binder<>();
DateField bDay = new DateField("Birthday");
bDay.setValue(LocalDate.now());
binder.forField(bDay).bind(Person::getBday, Person::setBday);
setContent(bDay);

talking about nullpointer, where are you seting your bean ?
maybe something with that ?

Hi Vilius,

thanks for your answer.
As I wrote in the first line of the 1st post I use Vaadin 8.4.1.

  1. Yes, you have right. I left the code this way when I migrated from V7.x to V8.
    Already modified. Thanks for the tip.

    However, here I have a cuestion:
    it is possible to optimize the line with DecimalFormat?

    table.addColumn(Deposito::getPeso)
      .setId("pesoEntradaCol")
      .setCaption("Peso Entrada")
      .setRenderer((Renderer) new NumberRenderer(new DecimalFormat("#,##0.00"))); // <--
    

    Yes, in my demo project it is working just fine also, but the bean is not Entity. This is the
    main diference.

  2. In the Constructor of the Form I set

    deposito = new Deposito();
    initUI();
    
    // in initUI()
    private void initUI() {
     buildForm();
     depositoEntradaBinder.setBean(deposito);
     .
     .
    }
    
  3. I’m still getting

    java.io.NotSerializableException: java.time.format.DateTimeFormatter

I think i found it.
https://github.com/vaadin/framework/issues/10207

it’s your Renderer.

you are using one of the old Renderers
public LocalDateTimeRenderer(String formatPattern) { … }

replace it with something like this

public LocalDateTimeRenderer(DateTimeFormatter formatter) { … }

Talking about optimisation.

In my project, i usualy need those number Formaters in many places, so i write a Class for it, and reference it as renderer.

Something like this

public class NumberConverter {

    public static String price(final Number number) {
        return new DecimalFormat("#,##0.00").format(number);
    }
}

IT is your Format from before, i just called it price.

If your Output is String, you can simply use the Presentation Provider by caling it in the addColumnMethod as second parameter.

  table.addColumn(Deposito::getPeso,  NumberConverter::price)
  .setId("pesoEntradaCol")
  .setCaption("Peso Entrada");

reusable for Future :slight_smile: