{"record":{"id":"7652c0443047db98","repo":"HandyOrg/HandyControl","slug":"getconvertfromexception","errorCode":null,"errorMessage":"GetConvertFromException","messagePattern":"GetConvertFromException","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/Avalonia/HandyControl_Avalonia/Tools/Converter/ColLayoutConverter.cs","lineNumber":28,"sourceCode":"    public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)\n    {\n        return Type.GetTypeCode(sourceType) switch\n        {\n            TypeCode.Int16 or TypeCode.UInt16\n                or TypeCode.Int32 or TypeCode.UInt32\n                or TypeCode.Int64 or TypeCode.UInt64\n                or TypeCode.Single or TypeCode.Double or TypeCode.Decimal\n                or TypeCode.String => true,\n            _ => false\n        };\n    }\n\n    public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)\n        => destinationType == typeof(string);\n\n    public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)\n    {\n        if (value == null) throw GetConvertFromException(null!);\n\n        return value switch\n        {\n            string s => ColLayout.Parse(s),\n            double d => new ColLayout((int) d),\n            _ => new ColLayout(System.Convert.ToInt32(value, culture))\n        };\n    }\n\n    public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)\n    {\n        if (value is ColLayout layout && destinationType == typeof(string))\n        {\n            return layout.ToString();\n        }\n        throw new ArgumentException(\"CannotConvertType\");\n    }\n}","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Avalonia/HandyControl_Avalonia/Tools/Converter/ColLayoutConverter.cs#L10-L46","documentation":"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.","triggerScenarios":"Calling converter.ConvertFrom(context, culture, null) directly; XAML/data-binding pipeline supplying a null value where a ColLayout (column span spec) is expected.","commonSituations":"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.","solutions":["Ensure the bound/source value is non-null before conversion (e.g. FallbackValue in bindings)","Handle null in the binding path so the converter is never invoked with null","Catch the exception and substitute a default ColLayout"],"exampleFix":"// before\nvar layout = (ColLayout)converter.ConvertFrom(context, culture, rawValue);\n// after\nvar layout = rawValue == null ? ColLayout.Default : (ColLayout)converter.ConvertFrom(context, culture, rawValue);","handlingStrategy":"validation","validationCode":"if (value == null) return ColLayout.Default;","typeGuard":"static bool IsConvertible(object? v) => v is string or double or int;","tryCatchPattern":"try { return (ColLayout)converter.ConvertFrom(ctx, culture, value); } catch (Exception ex) when (ex.Message.Contains(\"convert from\")) { return ColLayout.Default; }","preventionTips":["Set FallbackValue on converter bindings","Null-check before ConvertFrom","Coerce missing data to defaults"],"tags":["avalonia","handycontrol","converter","null"],"backgroundTag":"null-argument","analyzedSha":"2c0875ebd67326e0c67282967e3e809c69282fee","analyzedAt":"2026-09-14T14:45:29.754Z","contentChangedAt":"2026-09-14T14:45:29.754Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}