]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fixed NPE when converter is null (#10043) 54/154/1
authorArtur Signell <artur@vaadin.com>
Wed, 24 Oct 2012 06:54:59 +0000 (09:54 +0300)
committerArtur Signell <artur@vaadin.com>
Wed, 24 Oct 2012 06:54:59 +0000 (09:54 +0300)
Change-Id: I1d753a4da16feae0c83d755e9dcceae86f3dfbdc

server/src/com/vaadin/ui/AbstractField.java
server/tests/src/com/vaadin/tests/server/component/abstractfield/AbstractFieldValueConversions.java

index 59f730f2754725f4d84553e111b0a2dd61d8a99f..53301f093a16aebc2122e17cf5b9244833e5afa5 100644 (file)
@@ -687,19 +687,18 @@ public abstract class AbstractField<T> extends AbstractComponent implements
      */
     private Object convertToModel(T fieldValue)
             throws Converter.ConversionException {
+        Class<?> modelType = null;
+        Property pd = getPropertyDataSource();
+        if (pd != null) {
+            modelType = pd.getType();
+        } else if (getConverter() != null) {
+            modelType = getConverter().getModelType();
+        }
         try {
-            Class<?> modelType = null;
-            Property pd = getPropertyDataSource();
-            if (pd != null) {
-                modelType = pd.getType();
-            } else if (getConverter() != null) {
-                modelType = getConverter().getModelType();
-            }
             return ConverterUtil.convertToModel(fieldValue,
                     (Class<Object>) modelType, getConverter(), getLocale());
         } catch (ConversionException e) {
-            throw new ConversionException(
-                    getConversionError(converter.getModelType()), e);
+            throw new ConversionException(getConversionError(modelType), e);
         }
     }
 
index 73f6063fdda8393680fed014de34064982601730..83224861b68daf37a7332463c3a0f63be0accf62 100644 (file)
@@ -2,10 +2,15 @@ package com.vaadin.tests.server.component.abstractfield;
 
 import java.util.Locale;
 
+import junit.framework.Assert;
 import junit.framework.TestCase;
 
+import org.junit.Test;
+
 import com.vaadin.data.util.MethodProperty;
+import com.vaadin.data.util.ObjectProperty;
 import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.Converter.ConversionException;
 import com.vaadin.data.util.converter.StringToIntegerConverter;
 import com.vaadin.server.VaadinServiceSession;
 import com.vaadin.tests.data.bean.Address;
@@ -179,4 +184,19 @@ public class AbstractFieldValueConversions extends TestCase {
 
     }
 
+    @Test
+    public void testNullConverter() {
+        TextField tf = new TextField("foo");
+        tf.setPropertyDataSource(new ObjectProperty<Integer>(12));
+        tf.setConverter((Converter) null);
+        try {
+            Object v = tf.getConvertedValue();
+            System.out.println(v);
+            Assert.fail("Trying to convert String -> Integer should fail when there is no converter");
+        } catch (ConversionException e) {
+            // ok, should happen when there is no converter but conversion is
+            // needed
+        }
+    }
+
 }