Question

I have this simple Bean class and try to set some values with BeanUtils.setProperty Problem is, it seems that String works just fine, but when I try to set a Boolean value it just does not work. I have tried and define the field as public but still not working. Any help? Why is this not working?

public class TestBean {

protected Boolean someBoolean;
protected String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public boolean isSomeBoolean() {
    if (someBoolean == null) {
        return true;
    } else {
        return someBoolean;
    }
}

public void setSomeBoolean(Boolean value) {
    this.someBoolean = value;
}

public static void main(String[] args) {
    TestBean o = new TestBean();
    Boolean b = new Boolean(false);
    BeanUtils.setProperty(o, "someBoolean", b);
    BeanUtils.setProperty(o, "name", "A name");
    System.out.println(((TestBean)o).isSomeBoolean());
    // Output = true WHY?????
    System.out.println(((TestBean)o).getName());
    // Output = A name 

    BeanUtils.setProperty(o, "someBoolean", false);
    BeanUtils.setProperty(o, "name", "Another name");

    System.out.println(((TestBean)o).isSomeBoolean());
    // Output = true WHY????
    System.out.println(((TestBean)o).getName());
    // Output = Another name        

}

}

Was it helpful?

Solution

You need to change it from

protected Boolean someBoolean;

to

protected boolean someBoolean;

You will get more info from here.

Java Beans, BeanUtils, and the Boolean wrapper class

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top