Aleksander
(Aleksander Temlin)
May 7, 2018, 6:25am
1
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()
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.
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
Aleksander
(Aleksander Temlin)
May 7, 2018, 6:32am
2
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")); `
Vilius2
(Vilius Kukanauskas)
May 7, 2018, 7:26am
3
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
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 ?
Aleksander
(Aleksander Temlin)
May 7, 2018, 2:37pm
4
Hi Vilius,
thanks for your answer.
As I wrote in the first line of the 1st post I use Vaadin 8.4.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.
In the Constructor of the Form I set
deposito = new Deposito();
initUI();
// in initUI()
private void initUI() {
buildForm();
depositoEntradaBinder.setBean(deposito);
.
.
}
I’m still getting
java.io.NotSerializableException: java.time.format.DateTimeFormatter
Vilius2
(Vilius Kukanauskas)
May 7, 2018, 8:50pm
5
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