PropertyDescriptor - Exception with SetValue() while GetValue() works

3k views Asked by At

I have an object that I have defined and I wish to modify one of its property called "DeviceType" using PropertyDescriptor but it's not working out for me.

I can retrieve the value of the property DeviceType but when I use SetValue() to change its property value, I always get the exception: "Object reference not set to an instance of an object" even if I try to set the property to the same value that I just retrieved.

Here is some code:

PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(theBase);
var objValue = properties["DeviceType"].GetValue(theBase);

try 
{
    if (theBase != null && objValue != null)
    {
        properties["DeviceType"].SetValue(theBase, objValue);
    }
}
catch (Exception ex)
{

}

Any help would be much appreciated. Thanks.

1

There are 1 answers

0
G24T On BEST ANSWER

The property was not initialized correctly when my object was created and so the setter property was generating a null reference exception error.