Grid null value representation

When I click on an line to edit it, and thus enter edit mode, for some reason null values display as “null”. Why? What is really weird it is only columns/fields where I did column.setEditable(true);.

Code that sets null representation to blank.

		column = addTextColumn(grid, propertyId, caption);
		if(column.getEditorField() instanceof TextField)
		{
			TextField field = (TextField) column.getEditorField();
			field.setNullRepresentation("");
		}
				
		if( Arrays.asList(EDITABLE_COLUMNS).contains(propertyId) )
			column.setEditable(true); // These columns have the null problem
		else
			column.setEditable(false); // These problems do not have the null problem

And then we have the addTextColumn interface method, which, by default, sets it to blank as well:

	default Column addTextColumn( Grid grid, Object propertyId, String caption )
			throws IllegalStateException {
		Column column = addColumn(grid, propertyId, caption, String.class);
		TextRenderer renderer = new TextRenderer();
		column.setRenderer(renderer);

		return column;
	}

The TextRenderer() constructor does this (copied from eclipse, to save time, but this is normal Vaadin 7 library, nothing custom):

    /**
     * Creates a new text renderer
     */
    public TextRenderer() {
        this("");
    }

    /**
     * Creates a new text renderer
     *
     * @param nullRepresentation
     *            the textual representation of {@code null} value
     */
    public TextRenderer(String nullRepresentation) {
        super(String.class, nullRepresentation);
    }

As you can see from all of the above code, it should display blank, but it displays “null”:

No matter what I do, null representation is ignored in the editor. I even tried field.setNullRepresentation("NULLVALUE!!"); and it still displayed “null” in the editor. So the problem seems to be the editor. Any ideas?

Ok, this fixes it:

	default Column addTextColumn( Grid grid, Object propertyId, String caption )
			throws IllegalStateException {
		Column column = addColumn(grid, propertyId, caption, String.class);
		TextRenderer renderer = new TextRenderer();
		column.setRenderer(renderer);
				
		TextField field = new TextField();
		field.setNullRepresentation("");
		column.setEditorField(field);

		return column;
	}

No idea why my other things did not work, especially the below section:

		column = addTextColumn(grid, propertyId, caption);
		if(column.getEditorField() instanceof TextField)
		{
			TextField field = (TextField) column.getEditorField();
			field.setNullRepresentation("");
		}

But this does work, so I am good now, and maybe this will help someone else.

But this does work, so I am good now, and maybe this will help someone else.

I have not checked this for a while. But you seem to use Vaadin 7. And in Vaadin 7 Grid Editor has a default field factory (not in Vaadin 8 anymore), and it could be that it re-creates new fields from the scratch each time (code below), unless you have defined your own field component (the code above). Thus setting the null representation does not have an effect.