Parts-in-Place / Vaadin_TableExport

Automatically exported from code.google.com/p/tableexport-for-vaadin
0 stars 0 forks source link

nullpointer exception with null property and using table formatproperty #10

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
from https://vaadin.com/forum/-/message_boards/view_message/579716

That resolved my issue. Thanks. However, I'm getting a null pointer exception 
when my property value is null:

1
2
3
4
5
6
Caused by: java.lang.NullPointerException
        at com.vaadin.addon.tableexport.ExcelExport.getProperty(ExcelExport.java:550)
        at com.vaadin.addon.tableexport.ExcelExport.addDataRow(ExcelExport.java:501)
        at com.vaadin.addon.tableexport.ExcelExport.addDataRows(ExcelExport.java:446)
        at com.vaadin.addon.tableexport.ExcelExport.convertTable(ExcelExport.java:250)
        at com.vaadin.addon.tableexport.TableExport.export(TableExport.java:42)

That's the line in getProperty when we use table format property values:

1
2
3
4
5
                    final String formattedProp =
                            ((ExportableFormattedProperty) table).getFormattedPropertyValue(
                                    rootItemId, propId, prop);
                    if (!prop.getValue().toString().equals(formattedProp)) {
                        prop = new ObjectProperty<String>(formattedProp, String.class);

In my case, both formattedProp and prop.getValue() are null. This seems to work 
and is allowed for a Table. So perhaps an appropriate check for prop.getValue() 
being null is needed, unless it's not actually correct what the Vaadin code is 
allowing.

In IndexedContainer.IndexedContainerProperty.toString(), if the value is null, 
it returns null.

1
2
3
4
5
6
7
        public String toString() {
            final Object value = getValue();
            if (value == null) {
                return null;
            }
            return value.toString();
        }

But I'm not sure if that's a Vaadin bug or not since the property.toString() is 
called by Table.formatPropertyValue() which returns "" for a null property, but 
it makes no such determination for a null property value:

1
2
3
4
5
6
7
    protected String formatPropertyValue(Object rowId, Object colId,
            Property property) {
        if (property == null) {
            return "";
        }
        return property.toString();
    }

UPDATED: I can fix my code so that formattedProp is never null by capturing 
what Table.formatPropertyValue() returns, but that won't solve the bug since 
prop.getValue() may return null and that I believe is always valid.

Original issue reported on code.google.com by jnas...@gmail.com on 12 Oct 2011 at 2:39

GoogleCodeExporter commented 8 years ago
Fixed. This was a result of the following issue.  We identify whether a cell 
should use formatPropertyValue or not by whether formatPropertyValue returns 
something different than just property.toString().  If yes, then we use the 
formatPropertyValue format, but it is always a String.  If no, then we use the 
regular property value, and it is set properly according to the data type.  A 
problem happens when we have a null when we can't do this check.  So now, we 
just use formatPropertyValue (if the use formatPropertyValue flag is set) if it 
is null and we end up with an empty string instead of a null date or something 
like that.  So, if there is a null property in a column of dates, there will be 
an empty String cell in the Excel column.  This is probably fine.

One side-effect I noticed of enabling formatPropertyValue is that, when used to 
format numbers, it causes the totals cell not to work, because the totals 
formula is summing up Strings.  

Original comment by jnas...@gmail.com on 13 Oct 2011 at 12:04