writeBean() NullPointerException when trying to save object from form

I am trying to save a new Ticket after from my TicketForm. I can edit and save existing tickets i have hardcoded via my datagenerator, but I cannot save a new Ticket. When trying to save a new Ticket I get this error java.lang.NullPointerException: bean cannot be null at java.base/java.util.Objects.requireNonNull(Objects.java:247) ~[na:na] at com.vaadin.flow.data.binder.Binder.doWriteIfValid(Binder.java:2283) ~[flow-data-23.2.5.jar:23.2.5] at com.vaadin.flow.data.binder.Binder.writeBean(Binder.java:2197) ~[flow-data-23.2.5.jar:23.2.5] at com.TreeGridManyToManyTest.demo.TicketForm.validateAndSave(TicketForm.java:85) ~[classes/:na]

public class TicketForm extends FormLayout {
    TextField name = new TextField("Ticket Name");
    MultiSelectComboBox<Employee> tickets = new MultiSelectComboBox<>("Assigned Employees");
   // save, delete, close buttons here ....
    private Ticket ticket;
   Binder<Ticket> binder = new BeanValidationBinder<>(Ticket.class);
public TicketForm(List<Employee> employeeList) {
    binder.bindInstanceFields(this);
    // configure MultiSelectComboBox and add() here ....}
private HorizontalLayout createButtonLayout() {
    // fire events
        save.addClickListener(e -> validateAndSave());
        delete.addClickListener(e -> fireEvent(new TicketForm.DeleteEvent(this, ticket)));
        close.addClickListener(e -> fireEvent(new TicketForm.CloseEvent(this)));
        return new HorizontalLayout(save, delete, close);}
private void validateAndSave(){
        try {
            binder.writeBean(ticket);
            fireEvent(new TicketForm.SaveEvent(this, ticket));
        } catch (ValidationException e) {
            throw new RuntimeException(e);
        }}
  public void setTicket(Ticket ticket){
        this.ticket = ticket;
        tickets.deselectAll();
        binder.readBean(ticket);
        if (ticket != null ) { tickets.select(ticket.getEmployees());}}
// Vaadin CompnentEvent<TicketForm> abstract here ...
}

i dont understand why i can edit and save hardcoded Ticket objects i made in the datagenerator but I cannot save a new Ticket from the form

Seems like the ticket instance you are trying to write in is null. Did you pass a new instance of Ticket to the form?

in my view file i have the saveTicket() method:

 private void saveTicket(TicketForm.SaveEvent e) {
              e.getTicket().setEmployees(ticketForm.getComboBoxEmployees());

        ticketService.saveTicket(e.getTicket());
        updateList();
    }``` 
is this what you mean?

He’s asking about your call of “setTicket(Ticket)”

ah sorry

in my ticketForm i have

   public void setTicket(Ticket ticket){
        this.ticket = ticket;
        tickets.deselectAll();
        // readBean() to bind values in ticket object to populate form
        binder.readBean(ticket);
        if (ticket != null ) { tickets.select(ticket.getEmployees());}

    }

I do call it in the view file when i click the close Button

  private void closeAssignedEmployeeGrid(){
        ticketForm.setTicket(null);
        assignedEmployees.setVisible(false);
    }```

And if you wanna create a new ticket you call it with “new Ticket()”?

should be passed to here

 private void populateAssignedEmployeeGrid(Ticket ticket) {
        // no ticket = close grid. Ticket exists, display grid
        if (ticket == null) {
            closeAssignedEmployeeGrid();
        } else {
            ticketForm.setTicket(ticket);
            assignedEmployees.setItems(ticket.getEmployees());
            assignedEmployees.setVisible(true);
        }
    }```

Well it’s not passed if it’s null :wink:

when i pass java ticketForm.setTicket(new Ticket); in this method it breaks

What breaks? That’s the way how it’s supposed to work

My page wont load

heres a screenshot

Screenshot_132.png

How it should look
Screenshot_134.png

When i select “bug #1” from the grid on the bottom left, it does populate the form correctly

^line 95 sets null as list of items which isn’t allowed