dotnet/maui · error · InvalidOperationException

Cannot convert "{0}" into {1}

Error message

Cannot convert "{0}" into {1}

What it means

DoubleCollectionConverter.ConvertFrom throws InvalidOperationException when value.ToString() returns null. Because value is boxed object, ToString() normally yields a non-null string; reaching the null check requires a custom type whose ToString() returns null. The message formats the null and the DoubleCollection type.

Source

Thrown at src/Controls/src/Core/DoubleCollectionConverter.cs:32

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

		public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
		{
			if (value is double[] doublesArray)
			{
				return (DoubleCollection)doublesArray;
			}
			else if (value is float[] floatsArray)
			{
				return (DoubleCollection)floatsArray;
			}

			var strValue = value.ToString();
			if (strValue is null)
			{
				throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", strValue, typeof(DoubleCollection)));
			}

			string[] doubles = strValue.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
			var doubleCollection = new DoubleCollection();

			foreach (string d in doubles)
			{
				if (double.TryParse(d, NumberStyles.Number, CultureInfo.InvariantCulture, out double number))
				{
					doubleCollection.Add(number);
				}
				else
				{
					throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", d, typeof(double)));
				}
			}

			return doubleCollection;

View on GitHub (pinned to f377ff1c5e)

Solutions

  1. Pass a comma- or space-separated string of doubles (e.g. '1.5,2.5,3.5').
  2. Ensure any source object's ToString() never returns null.
  3. Pre-convert the source to a double[] or float[] which the converter also accepts.

Example fix

// before
var dc = (DoubleCollection)converter.ConvertFrom(objWithNullToString);

// after
var dc = (DoubleCollection)new[] { 1.5, 2.5 }; // accepted array path
Defensive patterns

Strategy: validation

Validate before calling

var s = value?.ToString();
if (s is null) throw new ArgumentException("value.ToString() is null; cannot convert to DoubleCollection.");

Prevention

When it happens

Trigger: Passing an object whose ToString() override returns null to a DoubleCollection-typed property; binding a null-yielding source through the converter.

Common situations: Custom value types overriding ToString to return null; edge null-boxing paths.

Related errors


AI-assisted analysis of dotnet/maui@f377ff1c5e (2026-08-13). Data as JSON: /api/errors/5e930698bc23bcdb. Report an issue: GitHub.