{"record":{"id":"67112a2817c0c041","repo":"HandyOrg/HandyControl","slug":"getconvertfromexception-null","errorCode":null,"errorMessage":"GetConvertFromException(null)","messagePattern":"GetConvertFromException\\(null\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Shared/HandyControl_Shared/Tools/Converter/ColLayoutConverter.cs","lineNumber":38,"sourceCode":"            case TypeCode.UInt32:\n            case TypeCode.Int64:\n            case TypeCode.UInt64:\n            case TypeCode.Single:\n            case TypeCode.Double:\n            case TypeCode.Decimal:\n            case TypeCode.String:\n                return true;\n            default:\n                return false;\n        }\n    }\n\n    public override bool CanConvertTo(ITypeDescriptorContext typeDescriptorContext, Type destinationType) =>\n        destinationType == typeof(InstanceDescriptor) || destinationType == typeof(string);\n\n    public override object ConvertFrom(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object source)\n    {\n        if (source == null) throw GetConvertFromException(null);\n\n        return source switch\n        {\n            string s => FromString(s, cultureInfo),\n            double d => new ColLayout((int) d),\n            _ => new ColLayout(Convert.ToInt32(source, cultureInfo))\n        };\n    }\n\n    [SecurityCritical]\n    public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object value, Type destinationType)\n    {\n        if (value == null) throw new ArgumentNullException(nameof(value));\n\n        if (destinationType == null) throw new ArgumentNullException(nameof(destinationType));\n\n        if (!(value is ColLayout th)) throw new ArgumentException(\"UnexpectedParameterType\");\n","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Shared/HandyControl_Shared/Tools/Converter/ColLayoutConverter.cs#L20-L56","documentation":"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.","triggerScenarios":"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.","commonSituations":"Missing ColLayout value in XAML (empty attribute), data binding supplying null to a property whose converter is ColLayoutConverter, or serialization round-trips passing null.","solutions":["Supply a non-null string or numeric value where a ColLayout is expected in XAML/binding.","Guard the call site: only invoke ConvertFrom when the source value is non-null.","If null is legitimate, subclass the converter and map null to a default ColLayout.","Check for typos/missing values in the XAML attribute that feeds the converter."],"exampleFix":"// before\npublic override object ConvertFrom(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object source)\n{\n    if (source == null) throw GetConvertFromException(null);\n// after\npublic override object ConvertFrom(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object source)\n{\n    if (source == null) return ColLayout.Default; // or keep the throw if null is truly invalid","handlingStrategy":"validation","validationCode":"if (value == null)\n{\n    // handle before invoking the converter\n    layout = ColLayout.Default;\n}\nelse\n{\n    layout = (ColLayout)new ColLayoutConverter().ConvertFrom(value);\n}","typeGuard":"bool IsValidColLayoutSource(object source) => source is string || source is double || source is int;","tryCatchPattern":"try\n{\n    var layout = (ColLayout)new ColLayoutConverter().ConvertFrom(source, culture, value);\n}\ncatch (NotSupportedException)\n{\n    layout = ColLayout.Default; // or log the null/unsupported source\n}","preventionTips":["Never pass null sources to ConvertFrom; default them at the binding/config layer.","Validate XAML attributes that feed ColLayoutConverter during CI builds.","Wrap manual converter calls in validation of source type first."],"tags":["wpf","typeconverter","xaml","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"}