5 Replies Latest reply: Feb 16, 2013 1:15 PM by Christian Sadilek RSS

Binder.setModel with InitialState.FROM_MODEL causes NPE in Convert

Thomas Frühbeck Newbie

I use Binder.setModel(bean, InitialState.FROM_MODEL) to set values in view.

When passing bean with properties != String I get a NPE in Convert class although I do not need conversion at all:

 

https://github.com/errai/errai/blob/master/errai-data-binding/src/main/java/org/jboss/errai/databinding/client/api/Convert.java

 

  public static Object to(Class toType, Object o) {    

     Assert.notNull(toType);     

     if (toType == String.class && o == null)  {

           o = "";    

     }     

 

     Assert.notNull(o);

 

java.lang.NullPointerException: null
  org.jboss.errai.common.client.api.Assert.notNull(Assert.java:56)
  org.jboss.errai.databinding.client.api.Convert.to(Convert.java:71)
  org.jboss.errai.databinding.client.api.Convert.toWidgetValue(Convert.java:165)
  org.jboss.errai.databinding.client.api.Convert.toWidgetValue(Convert.java:125)
  org.jboss.errai.databinding.client.BindableProxyAgent.updateWidgetAndFireEvent(BindableProxyAgent.java:334)
  org.jboss.errai.databinding.client.BindableProxyAgent.syncState(BindableProxyAgent.java:392)
  org.jboss.errai.databinding.client.BindableProxyAgent.bind(BindableProxyAgent.java:197)
  org.jboss.errai.databinding.client.BindableProxyAgent.copyStateFrom(BindableProxyAgent.java:96)
  org.jboss.errai.databinding.client.api.DataBinder.setModel(DataBinder.java:264)
  com.test.client.ui.EntityEditFormBase.updateView(EntityEditFormBase.java:113)
  com.test.client.ui.EmployeeEditForm.updateView(EmployeeEditForm.java:123)
  com.test.client.ui.EmployeeEditForm.updateView(EmployeeEditForm.java:1)
  com.test.client.ui.EntityEditFormBase$5$1.callback(EntityEditFormBase.java:221)

 

e.g. the DateBox is able to handle null, but the above Assertion seems to force me to write a <Date,Date> DefaultConverter, and perhaps many more??

 

Is this the planned behavior and is there a simple work around?

  • 1. Re: Binder.setModel with InitialState.FROM_MODEL causes NPE in Convert
    Christian Sadilek Master

    Hi Thomas,

     

    It does seem this was implemented too defensively. I pushed a change that allows for null values for all types in the default conversion utility. If you update your 2.2.1-SNAPSHOTs you should be able to use the change immediately.

     

    Cheers,

    Christian

  • 2. Re: Binder.setModel with InitialState.FROM_MODEL causes NPE in Convert
    Thomas Frühbeck Newbie

    Hi Christian,

     

    sorry for molesting :-)

    two more questions:

         - is there a specific reason to map null String to ""

              of course it would be unpleasant to change if users already rely of implicit Null mapping

     

         - Class identity check does not account for inheritance

        if (toType.equals(o.getClass())) {

          return o;

        }


    I would propose to use 

         if (toType.isAssignableFrom(o.getClass()))

     

    Thanks,

    Thomas

  • 3. Re: Binder.setModel with InitialState.FROM_MODEL causes NPE in Convert
    Christian Sadilek Master

    Hi Thomas,

     

    I don't think isAssignableFrom is part of GWT's JRE emulation library. We could work around that but I am not sure what the use case would be here. Looking at all HasValue<T> implementations in the default GWT widget set we find: String, Date, Boolean, Double, Integer, and Long. The Convert utility class is only used for simple built-in conversions using those types. Anything more complicated requires a custom converter. Is there a specific built-in converstion you are missing? If so, we should add a conversion rule for it.

     

    To your first question: I did some git archeology and found the commit that introducd the null to "" mapping for Strings. You are right, it looks like this conversion is no longer required. However, it doesn't seem to make a difference as textBox.setValue(null) will cause textBox.getValue() to return an empty string. So, at least in our way it's symmetric. Does this cause problems on your end? If so, it looks like we can change that.

     

    Cheers,

    Christian

  • 4. Re: Binder.setModel with InitialState.FROM_MODEL causes NPE in Convert
    Thomas Frühbeck Newbie

    Hi Christian,

    thank you for the detailed explanation - isAssignableFrom: I totally forgot, where all this is happening :-)

    For completeness I would propose to add BigDecimal and BigInteger, which both are supported by GWT JRE:

     

          else if (toType.equals(BigDecimal.class)) {

            return new BigDecimal((String) o);

          }

          else if (toType.equals(BigInteger.class)) {

            return new BigInteger((String) o);

          }


    WDYT?

     

    for Strings: this is really an old PITA, if you take into account, that in the end the server's DB (like Oracle) will turn "" into NULL, effectively destroying any effort on differentiating.

     

    Best regards,

    Thomas

  • 5. Re: Binder.setModel with InitialState.FROM_MODEL causes NPE in Convert
    Christian Sadilek Master

    Hi,

     

    Yes, good idea! We will add built-in conversions for BigDecimal and BigInteger.

     

    Thanks,

    Christian