Cannot set Boolean value through reflection

java reflection set field value
how to pass boolean value in java method
java lang illegalargumentexception can not set java lang string field
boolean true

I am not able to set a Boolean value to a field using Java reflection. The field data type is java.lang.Boolean. however, I am able to set the value if the data type is primitive type i.e boolean.

Here is a simple VO with Boolean type and primitive type:

public class TestVO {
    private Boolean bigBoolean;
    private boolean smallBoolean;
}

Here is my java reflection code:

public class TestClass {
    public static void main(String args[])
            throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
        TestVO testVO1 = new TestVO();

        Class testVO = testVO1.getClass();
        Field smallBooleanField = TestVO.class.getDeclaredField("smallBoolean");
        Field bigBooleanField = TestVO.class.getDeclaredField("bigBoolean");

        String name1 = smallBooleanField.getName();
        System.out.println("SmallBoolean Fieldname is: " + name1);

        smallBooleanField.setAccessible(true);

        // get the value of this private field
        Boolean fieldValue = (Boolean) smallBooleanField.get(testVO1);
        System.out.println("fieldValue = " + fieldValue);

        smallBooleanField.setAccessible(true);
        smallBooleanField.setBoolean(testVO1, true);

        // get the value of this private field
        fieldValue = (Boolean) smallBooleanField.get(testVO1);
        System.out.println("fieldValue = " + fieldValue);

        name1 = bigBooleanField.getName();
        System.out.println("bigBooleanField Fieldname is: " + name1);

        bigBooleanField.setAccessible(true);

        // get the value of this private field
        fieldValue = (Boolean) bigBooleanField.get(testVO1);
        System.out.println("fieldValue = " + fieldValue);

        bigBooleanField.setAccessible(true);
        bigBooleanField.setBoolean(testVO1, new Boolean(true));

        // get the value of this private field
        fieldValue = (Boolean) bigBooleanField.get(testVO1);
        System.out.println("fieldValue = " + fieldValue);

    }
}

Output is:

SmallBoolean Fieldname is: smallBoolean
fieldValue = false
fieldValue = true
bigBooleanField Fieldname is: bigBoolean
fieldValue = null
Exception in thread "main" java.lang.IllegalArgumentException: Can not set java.lang.Boolean field TestVO.bigBoolean to (boolean)true
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:175)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.setBoolean(UnsafeObjectFieldAccessorImpl.java:90)
    at java.lang.reflect.Field.setBoolean(Field.java:795)
    at TestClass.main(TestClass.java:44)

I tried to set the bigBoolean value with new Boolean(true), Boolean.TRUE, true etc. nothing works.

According to this, bigBoolean.setBoolean() is invoked to set a field that is of the reference type Boolean with a value of primitive type. In the non-reflection equivalent Boolean val = true, the compiler would convert (or box) the primitive type 'true ' to a reference type as new Boolean(True) so that its type checking will accept the statement.

When using reflection, type checking only occurs at runtime so there is no opportunity to box the value. This forces to throw IllegalArgumentException due to Inconvertible Types

How to get and set field value through reflection in Java, How to get value from a field; How to set field value with Reflection; Value for a public final static field cannot be set through Java reflection getBoolean(Object obj) Gets field value as boolean. byte getByte(Object obj) Gets field value as byte. According to this,bigBoolean.setBoolean() is invoked to set a field that is of the reference type Boolean with a value of primitive type. In the non-reflection equivalent Boolean val = true, the compiler would convert (or box) the primitive type 'true ' to a reference type as new Boolean(True) so that its type checking will accept the statement.

Change this line and it should work for you:

bigBooleanField.set(testVO1, Boolean.TRUE);

Java Reflection - Field.setBoolean() Examples, public void setBoolean(Object obj, boolean z) throws IllegalArgumentException, IllegalAccessException. Sets the value of a field as a boolean on the specified  For example, if the field's type is Boolean, an instance of Object with the appropriate Boolean value is passed. Before setting the value, SetValue checks to see if the user has access permission. This method cannot be used to set values of static, init-only (readonly in C#) fields reliably. In .NET Core 3.0 and later versions, an exception is thrown if you attempt to set a value on a static, init-only field.

I was facing the same issue and reason is not reflection reason is getter method type. If you notice for boolean/Boolean your IDE create a getter method as isVariableName() instead of getVariableName(). If you change your method name to get type it will work fine.

Java Reflection - Field.getBoolean() Examples, Gets the value of a static or instance boolean field. The provided 'obj' is the object to extract the boolean value from (should be null for static fields). Value for a public final static field cannot be set through Java reflection Get value from a field We can get field value with reflection methods and they returns primitive types.

The class java.lang.reflect.Field, The reflected field may be a class variable ( static field) or an instance variable If the underlying field's value cannot be converted to an int by an identity or a public void setBoolean(Object obj, boolean z) throws NullPointerException,  Imagine you have a bigint column in SQLServer, which has a value 50000000. When you retrieve this value from database, the underlying type would be long. If you set this value to property of integer datatype, you will get an “ Argument Exception ” shown below even though 50000000 can be

FieldInfo.SetValue Method (System.Reflection), Sets the value of the field supported by the given object. The value parameter cannot be converted and stored in the field. Reflection; using System. For example, if the field's type is Boolean, an instance of Object with the appropriate  PropertyAccessException: could not set a field value by reflection setter of com. test. dataobject. Shop . atShop It is throwing this because it is trying to set the boolean to null in the HSQL.

java.lang.reflect.Field.setBoolean() Method Example, setBoolean(Object obj, Object value) method sets the value of a field as a boolean on the specified object. Declaration. Following is the declaration for java.​lang. Field.setInt() is invoked to set a field that is of the reference type Integer with a value of primitive type. In the non-reflection equivalent Integer val = 42, the compiler would convert (or box) the primitive type 42 to a reference type as new Integer(42) so that its type checking will accept the statement.

Comments
  • Thanks for the detailed answer and for the solution. +1.