1. Understanding the Error Message:
The error message shown in the Assign activity states:"Object reference not set to an instance of an object."
Exception Type: System.NullReferenceException
This error occurs when trying to use a variable that has not been initialized (i.e., it is Nothing or null in UiPath).
2. Why is this happening?
The variable PinMapping is a Dictionary (Dictionary<String, String>), but it has not been initialized before adding a key-value pair.
In UiPath, a dictionary must be initialized before assigning values.
Since PinMapping is Nothing (null), attempting to access or modify it causes a NullReferenceException.
3. Why Not Other Options?
"The 'John' key was not present in the dictionary." ❌
This would cause a KeyNotFoundException, not a NullReferenceException.
"The assign's set value syntax should be PinMapping<"John">." ❌
"The assign's set value syntax should be PinMapping["John"]." ❌
The syntax is correct, but the actual issue is that the dictionary itself was not initialized.
4. Correcting the Issue:
Before using the dictionary, it must be initialized.
Solution 1: Initialize the Dictionary Before Use
In Variables Panel, set the default value of PinMapping as:
New Dictionary(Of String, String)
OR
Use an Assign activity before adding elements:
PinMapping = New Dictionary(Of String, String)
Solution 2: Check for Null Before Using
To avoid errors, check if PinMapping is Nothing:
If PinMapping Is Nothing Then
PinMapping = New Dictionary(Of String, String)
End If
Then, proceed with adding key-value pairs.
5. Reference from UiPath Official Documentation:
UiPath Dictionary Initialization
Handling NullReferenceException in UiPath