EVariantError
Delphi Programming Language
Severity: ModerateWhat Does This Error Mean?
EVariantError means an operation on a Variant type failed. A Variant is a special Delphi type that can hold almost any value — numbers, strings, dates, booleans, or nothing at all (Null or Unassigned). The error occurs when you try to use a Variant in a way that is incompatible with the value it currently holds, or when you operate on a Null or Unassigned Variant.
Affected Models
- All Delphi versions
- COM/OLE automation code
- Database code using Variants
Common Causes
- Performing arithmetic or string operations on a Null or Unassigned Variant
- Trying to convert a Variant to a specific type when the contained value cannot be converted
- Accessing a property of a Variant that holds a COM/OLE automation object that does not support that property
- A database field value being Null and the code not checking for Null before using it
- Comparing or combining Variants of incompatible types in a way that cannot be resolved
How to Fix It
-
Before using a Variant value, check it with VarIsNull(V) and VarIsEmpty(V). Both return True when the Variant contains no usable value.
VarIsNull checks for database-style Null. VarIsEmpty checks for Unassigned (the Variant was never given a value).
-
Use VarToStrDef(V, DefaultStr) instead of VarToStr(V) when converting a Variant to a string. The Def version returns a default string if the Variant is Null or Unassigned.
Example: DisplayName := VarToStrDef(AField.Value, 'Unknown');
-
For database field access, check for Null before reading: if not VarIsNull(MyTable.FieldValues['Amount']) then Amount := MyTable.FieldValues['Amount'];
Or use the safer TField approach: Amount := MyTable.FieldByName('Amount').AsFloat — TField handles Null conversion for you.
-
If you get EVariantError from COM/OLE automation code, check the documentation for the COM object to confirm which properties and methods it actually supports.
COM automation calls can also fail with EVariantError if the automation server is not running or not installed.
-
Use VarType(V) to check what type a Variant actually holds before operating on it. Compare against the varXxx constants (varInteger, varString, varNull, varEmpty, etc.).
Example: if VarType(V) = varNull then ... — this gives you fine-grained control over Variant handling.
When to Call a Professional
EVariantError is always something you can fix yourself. The key is to always check if a Variant is Null or Unassigned before using it. For database field values, VarIsNull() is your most important tool.
Frequently Asked Questions
What is the difference between Null and Unassigned in a Variant?
Unassigned means the Variant variable was declared but never given any value — it is completely empty. Null means the Variant was explicitly set to represent 'no value' (the SQL concept of NULL). In practice, both cause EVariantError if you try to use them in most operations. Check for both: if not (VarIsNull(V) or VarIsEmpty(V)) then use V.
Why does Delphi have a Variant type if it causes so many errors?
Variants are essential for COM/OLE automation and for interacting with components that can hold different types of data. They are also useful when reading database fields, since a field's value might be an integer, a string, a date, or Null. For most regular programming, use strongly-typed variables instead — they are safer and faster.
Is it possible to make Variant operations not raise exceptions?
Yes — you can set the NullStrictConvert global variable to False in the Variants unit. This makes some Null conversions return default values instead of raising exceptions. However, enabling this globally can hide bugs. It is better to explicitly check for Null before using Variant values.