HandyOrg/HandyControl · error · Exception

GetConvertFromException

Error message

GetConvertFromException

What it means

ColLayoutConverter.ConvertFrom calls TypeConverter.GetConvertFromException when the input value is null, producing an exception stating the converter cannot convert from the given object. The converter only accepts string, double, or numeric values and rejects anything else (null first). This is HandyControl's Avalonia ColLayout converter enforcing valid input.

Solutions

  1. Ensure the bound/source value is non-null before conversion (e.g. FallbackValue in bindings)
  2. Handle null in the binding path so the converter is never invoked with null
  3. Catch the exception and substitute a default ColLayout

Example fix

// before
var layout = (ColLayout)converter.ConvertFrom(context, culture, rawValue);
// after
var layout = rawValue == null ? ColLayout.Default : (ColLayout)converter.ConvertFrom(context, culture, rawValue);
Defensive patterns

Strategy: validation

Validate before calling

if (value == null) return ColLayout.Default;

Type guard

static bool IsConvertible(object? v) => v is string or double or int;

Try / catch

try { return (ColLayout)converter.ConvertFrom(ctx, culture, value); } catch (Exception ex) when (ex.Message.Contains("convert from")) { return ColLayout.Default; }

Prevention

When it happens

Trigger: Calling converter.ConvertFrom(context, culture, null) directly; XAML/data-binding pipeline supplying a null value where a ColLayout (column span spec) is expected.

Common situations: Binding to a property that is null at converter evaluation time; hand-written conversion code passing null; deserializing layouts where the source field is missing.

Related errors


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

Appendix: source

Thrown at src/Avalonia/HandyControl_Avalonia/Tools/Converter/ColLayoutConverter.cs:28

    public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
    {
        return Type.GetTypeCode(sourceType) switch
        {
            TypeCode.Int16 or TypeCode.UInt16
                or TypeCode.Int32 or TypeCode.UInt32
                or TypeCode.Int64 or TypeCode.UInt64
                or TypeCode.Single or TypeCode.Double or TypeCode.Decimal
                or TypeCode.String => true,
            _ => false
        };
    }

    public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
        => destinationType == typeof(string);

    public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
    {
        if (value == null) throw GetConvertFromException(null!);

        return value switch
        {
            string s => ColLayout.Parse(s),
            double d => new ColLayout((int) d),
            _ => new ColLayout(System.Convert.ToInt32(value, culture))
        };
    }

    public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
    {
        if (value is ColLayout layout && destinationType == typeof(string))
        {
            return layout.ToString();
        }
        throw new ArgumentException("CannotConvertType");
    }
}

View on GitHub (pinned to 2c0875ebd6)