简体   繁体   中英

How to initialize an entity which is represented by a Vaadin Grid's row?

I use Vaadin framework v23 for my project.

A wanted to add a component column to one of my Grids, a number field. My Grid's rows are representing an entity from the database my program connected to. I ask for an input from the user, and I want to write that input back to the DB. I found this solution at several sites:

private Grid<Honey> initHoneyGrid() {
    Grid<Honey> honeyGrid = new Grid<>(Honey.class);

    honeyGrid.setItems(honeys);
    honeyGrid.removeColumnByKey("id");
    honeyGrid.removeColumnByKey("quantityPacked");
    honeyGrid.removeColumnByKey("flatCost");
    honeyGrid.removeColumnByKey("sellingPrice");
    honeyGrid.setColumns("kindOfHoney", "weight", "quantityBack");

    honeyGrid.addComponentColumn(item -> new NumberField("Set the quantity you brought back'", event -> {
        item.setQuantityBack(event.getValue().intValue());
        honeyService.updateHoney(item);
        honeyGrid.getDataProvider().refreshAll();
    }));

    return honeyGrid;
}

However, the variable "item" throws NullPointerException. The "honeys" variable is an ArrayList, which is filled from the DB directly. I use Spring Boot JPA to access the database. How can I avoid it?

check docs here to see components and usage of them, the reason of NPE that you get is item might not be initialised, what you are trying to do now is assign some property that you don't have any value of it in item to new NumberField(...)

the lambda expression will already assign new NumberField(...) to ìtem at the end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM