Vaadin 8 grid nested object handling

currently I am writing a grid which should display an object which contains nested objects of which I want to display its name.
Currently I have the following code:

public class SwitchGrid extends Grid<IpSwitch> implements AutoRefresherComponent {
	private static final long serialVersionUID = -6611331292773081623L;
	// UI controller
	private SwitchGridUiControllerInterface uicontroller;
	public SwitchGrid(SwitchGridUiControllerInterface uicontroller) {
		this.uicontroller = uicontroller;
		this.setColumns("switchState", "enabled", "switchable", "name", "", "");

As you can see I am using the setcolumns() methods cisplay the columns I want, which works quit well. There is only one problem: The switchGroup object can be null, and in this case I get a null pointer exception. How can I define, that only if the nested object is not null, its name should be displayed, if it is null I want to display an empty string?


Hi Florian,

Could you please post the error message that you get when using the above sample?
Also, how do you create/instantiate the SwitchGrid in your code?

You could do i.e. something like:

grid.addColumn(ipSwitch -> {
	if (ipSwitch.getSwitchGroup() != null) {
	 return ipSwitch.getSwitchGroup().getName();
	return "";
}).setCaption("Switch group name").setId("");

Please note that I haven’t tried this code in an IDE :slight_smile: