You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Tj Zecca (JIRA)" <ji...@apache.org> on 2008/06/04 00:52:45 UTC

[jira] Created: (BEANUTILS-317) BeanUtils.setProperty does not seem to be able to set a null value on a java.lang.Integer

BeanUtils.setProperty does not seem to be able to set a null value on a java.lang.Integer
-----------------------------------------------------------------------------------------

                 Key: BEANUTILS-317
                 URL: https://issues.apache.org/jira/browse/BEANUTILS-317
             Project: Commons BeanUtils
          Issue Type: Bug
          Components: Bean / Property Utils
    Affects Versions: 1.8.0-BETA, 1.7.0, 1.6.1, 1.6
         Environment: Java 1.6
            Reporter: Tj Zecca


I get the following unexpected results:

INITIAL STATE: null
AFTER SETTER: 5
AFTER BeanUtils.setProperty: 0


Using the following code:

public static void main(String[] args) {
    SimpleBean bean = new SimpleBean();
    System.out.println("INITIAL STATE: " + bean.getNum());

    bean.setNum(new Integer(5));
    System.out.println("AFTER SETTER: " + bean.getNum());

    try {
        BeanUtils.setProperty(bean, "num", null);
        System.out.println("AFTER BeanUtils.setProperty: " + bean.getNum());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public class SimpleBean {
    private Integer num;

    public Integer getNum() {
        return this.num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }
}

PS. I have also tested with the Boolean primitive wrapper and it does not set null either.


-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Resolved: (BEANUTILS-317) BeanUtils.setProperty does not seem to be able to set a null value on a java.lang.Integer

Posted by "Niall Pemberton (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/BEANUTILS-317?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Niall Pemberton resolved BEANUTILS-317.
---------------------------------------

    Resolution: Invalid

Questions like this should really be asked on the user mailing list, rather than using the Bug tracking system.

BeanUtils uses the registered converter for the type to convert to the appropriate type. So you need to either configure the converter with a default value of null - or using PropertyUtils (which doesn't try to convert) instead:

So for the Converter solution, do the following:
        ConvertUtils.register(new IntegerConverter(null), Integer.class);

... or use PropertyUtils
        PropertyUtils.setProperty(foo, "num", null);



> BeanUtils.setProperty does not seem to be able to set a null value on a java.lang.Integer
> -----------------------------------------------------------------------------------------
>
>                 Key: BEANUTILS-317
>                 URL: https://issues.apache.org/jira/browse/BEANUTILS-317
>             Project: Commons BeanUtils
>          Issue Type: Bug
>          Components: Bean / Property Utils
>    Affects Versions: 1.6, 1.6.1, 1.7.0, 1.8.0-BETA
>         Environment: Java 1.6
>            Reporter: Tj Zecca
>
> I get the following unexpected results:
> INITIAL STATE: null
> AFTER SETTER: 5
> AFTER BeanUtils.setProperty: 0
> Using the following code:
> public static void main(String[] args) {
>     SimpleBean bean = new SimpleBean();
>     System.out.println("INITIAL STATE: " + bean.getNum());
>     bean.setNum(new Integer(5));
>     System.out.println("AFTER SETTER: " + bean.getNum());
>     try {
>         BeanUtils.setProperty(bean, "num", null);
>         System.out.println("AFTER BeanUtils.setProperty: " + bean.getNum());
>     } catch (Exception e) {
>         e.printStackTrace();
>     }
> }
> public class SimpleBean {
>     private Integer num;
>     public Integer getNum() {
>         return this.num;
>     }
>     public void setNum(Integer num) {
>         this.num = num;
>     }
> }
> PS. I have also tested with the Boolean primitive wrapper and it does not set null either.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (BEANUTILS-317) BeanUtils.setProperty does not seem to be able to set a null value on a java.lang.Integer

Posted by "Jeremy Mawson (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/BEANUTILS-317?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12609426#action_12609426 ] 

Jeremy Mawson commented on BEANUTILS-317:
-----------------------------------------

I'm having trouble with this also.

I am trying to set a null Integer onto an Integer, but BeanUtilsBean is defaulting the value to 0, which has negative side-effects for my app.

The BeanUtilsBean.copyProperty(Object, String, Object) method attempt to find and use a Converter in all circumstances. It probably should only do this if the source and target field types are different.


> BeanUtils.setProperty does not seem to be able to set a null value on a java.lang.Integer
> -----------------------------------------------------------------------------------------
>
>                 Key: BEANUTILS-317
>                 URL: https://issues.apache.org/jira/browse/BEANUTILS-317
>             Project: Commons BeanUtils
>          Issue Type: Bug
>          Components: Bean / Property Utils
>    Affects Versions: 1.6, 1.6.1, 1.7.0, 1.8.0-BETA
>         Environment: Java 1.6
>            Reporter: Tj Zecca
>
> I get the following unexpected results:
> INITIAL STATE: null
> AFTER SETTER: 5
> AFTER BeanUtils.setProperty: 0
> Using the following code:
> public static void main(String[] args) {
>     SimpleBean bean = new SimpleBean();
>     System.out.println("INITIAL STATE: " + bean.getNum());
>     bean.setNum(new Integer(5));
>     System.out.println("AFTER SETTER: " + bean.getNum());
>     try {
>         BeanUtils.setProperty(bean, "num", null);
>         System.out.println("AFTER BeanUtils.setProperty: " + bean.getNum());
>     } catch (Exception e) {
>         e.printStackTrace();
>     }
> }
> public class SimpleBean {
>     private Integer num;
>     public Integer getNum() {
>         return this.num;
>     }
>     public void setNum(Integer num) {
>         this.num = num;
>     }
> }
> PS. I have also tested with the Boolean primitive wrapper and it does not set null either.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.