HandyOrg/HandyControl · error

GetConvertFromException(null)

Error message

GetConvertFromException(null)

What it means

ColLayoutConverter.ConvertFrom throws the standard TypeConverter GetConvertFromException when source is null. This produces a NotSupportedException stating the converter cannot convert from (null), because a ColLayout cannot be constructed from a null source value.

Solutions

  1. Supply a non-null string or numeric value where a ColLayout is expected in XAML/binding.
  2. Guard the call site: only invoke ConvertFrom when the source value is non-null.
  3. If null is legitimate, subclass the converter and map null to a default ColLayout.
  4. Check for typos/missing values in the XAML attribute that feeds the converter.

Example fix

// before
public override object ConvertFrom(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object source)
{
    if (source == null) throw GetConvertFromException(null);
// after
public override object ConvertFrom(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object source)
{
    if (source == null) return ColLayout.Default; // or keep the throw if null is truly invalid
Defensive patterns

Strategy: validation

Validate before calling

if (value == null)
{
    // handle before invoking the converter
    layout = ColLayout.Default;
}
else
{
    layout = (ColLayout)new ColLayoutConverter().ConvertFrom(value);
}

Type guard

bool IsValidColLayoutSource(object source) => source is string || source is double || source is int;

Try / catch

try
{
    var layout = (ColLayout)new ColLayoutConverter().ConvertFrom(source, culture, value);
}
catch (NotSupportedException)
{
    layout = ColLayout.Default; // or log the null/unsupported source
}

Prevention

When it happens

Trigger: XAML or code calls the type converter with a null source: e.g. a ColLayout binding/attribute resolves to null, or ConvertFrom(null) is invoked directly in converter tests.

Common situations: Missing ColLayout value in XAML (empty attribute), data binding supplying null to a property whose converter is ColLayoutConverter, or serialization round-trips passing null.

Related errors


AI-assisted analysis of HandyOrg/HandyControl@2c0875ebd6 (2026-09-14). Data as JSON: /api/errors/67112a2817c0c041. Report an issue: GitHub.

Appendix: source

Thrown at src/Shared/HandyControl_Shared/Tools/Converter/ColLayoutConverter.cs:38

            case TypeCode.UInt32:
            case TypeCode.Int64:
            case TypeCode.UInt64:
            case TypeCode.Single:
            case TypeCode.Double:
            case TypeCode.Decimal:
            case TypeCode.String:
                return true;
            default:
                return false;
        }
    }

    public override bool CanConvertTo(ITypeDescriptorContext typeDescriptorContext, Type destinationType) =>
        destinationType == typeof(InstanceDescriptor) || destinationType == typeof(string);

    public override object ConvertFrom(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object source)
    {
        if (source == null) throw GetConvertFromException(null);

        return source switch
        {
            string s => FromString(s, cultureInfo),
            double d => new ColLayout((int) d),
            _ => new ColLayout(Convert.ToInt32(source, cultureInfo))
        };
    }

    [SecurityCritical]
    public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object value, Type destinationType)
    {
        if (value == null) throw new ArgumentNullException(nameof(value));

        if (destinationType == null) throw new ArgumentNullException(nameof(destinationType));

        if (!(value is ColLayout th)) throw new ArgumentException("UnexpectedParameterType");

View on GitHub (pinned to 2c0875ebd6)