Invoke Vb.NET Code dictionary error

Hello There,

I have the following code, which results in a Null reference error. This works well outside (e.g. in Visual Studio) but not in UiPath. Any ideas where i am doing it wrong ? Here is the error and code:

Error

System.NullReferenceException: Object reference not set to an instance of an object.
at UiPathCodeRunner_e44525bcc724414497acade96d68e114.Run(Dictionary2& emailDict, String str, List1 lst)
Object reference not set to an instance of an object.

Dim item, key, val As String

Try	
	For Each item In lst 'lst is a list passed as input
		key = item.Split(CChar(":"))(0).ToString.Trim
	    val = item.Split(CChar(":"))(1).ToString.Trim
	    emailDict.Add(key, val) 'dictionary defined as an out argument within UiPath
		
	Next
	
Catch e As Exception
	
	console.WriteLine(e)
	console.WriteLine(e.Message)
	console.WriteLine(e.Source)
	
End Try

Hi @Hara_Gopal

I think u had forget to declare the dictionary

Can u try this code

Dim dictionary As New Dictionary (Of String, String)

Dim item, key, val As String

Try	
	For Each item In lst 'lst is a list passed as input
		key = item.Split(CChar(":"))(0).ToString.Trim
	    val = item.Split(CChar(":"))(1).ToString.Trim
	    emailDict.Add(key, val)
		
	Next
	
Catch e As Exception
	
	console.WriteLine(e)
	console.WriteLine(e.Message)
	console.WriteLine(e.Source)
	
End Try

Regards

Nived N :robot:

Happy Automation :relaxed::relaxed::relaxed:

2 Likes

Hello Nived, The dictionary is an out argument already defined in UiPath

Hi @Hara_Gopal

In UiPath you have to create a new dictionary variable inside invoke code and assign that dictionary to your output dictionary variable.

Try this,

Dim dict As New Dictionary (Of String, String)

Dim item, key, val As String

Try
For Each item In lst 'lst is a list passed as input
key = item.Split(CChar(":"))(0).ToString.Trim
val = item.Split(CChar(":"))(1).ToString.Trim
dict.Add(key, val)

Next

emailDict = dict

Catch e As Exception

console.WriteLine(e)
console.WriteLine(e.Message)
console.WriteLine(e.Source)

End Try

2 Likes