Add a click handler to a table

Hello!
I am trying to create a table with data. If I klick on a record one value of this record the pid should be displayed somewhere. But however if I try to get the value of the selected record I get an null pointer exception. I hope someone can help me.

This is the code of the table:


public class PowershellGrid {
	
	private static final long serialVersionUID = -182164459252797609L;
	
	private Table grid;
	
	private Window mainWindow;
	
	private PowershellCommandContainer dataSource = PowershellCommandContainer.createContainer();
	
	public PowershellGrid(Window mainWindow){
		this.mainWindow = mainWindow;
	}
	
    public PowershellCommandContainer getDataSource() {
         return dataSource;
    }
    
    public Table createPowershellGrid(){
    	System.out.println("Hallo!!!!!!");
    	grid = new Table();
    	
    	grid.setWidth("650px");
    	grid.setColumnCollapsingAllowed(false);
    	
    	grid.setSelectable(true);
    	grid.setImmediate(true);
    	grid.setMultiSelect(false);
    	
    	grid.setContainerDataSource(this.getDataSource());
    	
    	grid.setVisibleColumns(PowershellCommandContainer.NATURAL_COL_ORDER);
        grid.setColumnHeaders(PowershellCommandContainer.COL_HEADERS_ENGLISH);
        grid.addListener(new Table.ValueChangeListener(){
        	
			private static final long serialVersionUID = -1661124568210657394L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				System.out.println("Hallo 2!!!!!!");
				
				mainWindow.getWindow().showNotification("Selected: "+grid.getValue());
			}});
        
        return grid;
    }
	
}

This is the code of the container:


public class PowershellCommandContainer extends BeanItemContainer<PowershellCommand> implements Serializable {

	private static Logger log = Logger.getLogger(PowershellCommandContainer.class);
	
	private static final long serialVersionUID = -7369746537444565111L;
	
	/**
	 * Natural property order for Person bean. Used in tables and forms.
	 */
	//public static final Object[] NATURAL_COL_ORDER = new Object[]
 {"pid", "creationDate", "powershellExe", "powershellCommand", "shortDescription", "longDescription" };
	public static final Object[] NATURAL_COL_ORDER = new Object[]
 {"pid", "creationDate", "shortDescription" };

	
	/**
	 * "Human readable" captions for properties in same order as in
	 * NATURAL_COL_ORDER.
	 */
	//public static final String[] COL_HEADERS_ENGLISH = new String[]
 {"PID", "Creation date", "Powershell exe", "Powershell command", "Short description", "Long description" };
	public static final String[] COL_HEADERS_ENGLISH = new String[]
 {"PID", "Creation date", "Short description" };
	
	public PowershellCommandContainer() throws InstantiationException,
			IllegalAccessException {
		super(PowershellCommand.class);
	}
	
	
	public static PowershellCommandContainer createContainer(){
		try {
			PowershellCommandContainer cont = new PowershellCommandContainer();
			List<PowershellCommand> commands  = PowershellCommandHibernate.getPowershellCommandList();
			
			for(int i = 0; i<commands.size(); i++){
				cont.addItem(commands.get(i));
			}
			return cont;
		}
		catch(Exception e){
			log.error(e.getMessage());
			return null;
		}
		
	}
}

Thanks!

I can’t be sure how you’ve this set up, but it mainWindow is indeed the main window, getWindow() will return null, so without even looking at the container, I’m guessing this is the culprit.
Try

mainWindow.showNotification("Selected: "+grid.getValue());

or even

System.out.println("Selected: "+grid.getValue());

HTH - otherwise a stacktrace would be nice :slight_smile:

Best Regards,
Marc