Vaadin 8 grid nested object handling

Hi,
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) {
		super(IpSwitch.class);
		this.setLocale(UI.getCurrent().getLocale());
		this.setSizeFull();
		
		this.uicontroller = uicontroller;
		
		this.setColumns("switchState", "enabled", "switchable", "name", "ipControlDevice.name", "switchGroup.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?

Thanks,
Florian

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("switchGroup.name");

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

Regards,
Goran