0

I have scoured the internet looking for a solution to this problem but the null exception remains. Basically I am getting System.NullReferenceException error when attempting to add values to a nested dictionary.

Here is my code.

Public Dict1 as New Dictionary(Of Integer, Dictionary(Of String, String))() 'Tried with and without()

Sub CreateDict(Length As Integer)

    Dim key As Integer
    Dim CmdName As String
    Dim CmdValue As String

    key = 0
    CmdName = "UnusedCmd"
    CmdValue = "NotAFolder,NotAFolder,NotAFolder,NotAFolder,NotAFolder,Read"

    While key <= Length
        Dim CmdDict As New Dictionary(Of String, String)
        CmdDict.Add("Name", CmdName) 'Method 1 of Adding
        CmdDict.Add("Command", CmdValue) 'Method 1 of Adding
        'CmdDict("Name") = CmdName 'Method 2 of Adding Both seem to work as evidenced by msgbox
        'CmdDict("Command") = CmdValue 'Method 2 of Adding Both seem to work as evidenced by msgbox
        MsgBox(CmdDict("Name")) ' Returns UnusedCmd
        MsgBox(CmdDict("Command"))' Returns NotAFolder,NotAFolder,NotAFolder,NotAFolder,NotAFolder,Read
        MsgBox(key) 'Returns 1
        Dict1.Add(key, CmdDict)
        key = key + 1
        CmdDict = Nothing 'Error Occurs whether or not this is commented out
    End While

End Sub

Note this code was adapted from my original project which was in VBA for outlook 2016. I am trying to readapt this code to be an outlook addin.

I have tried replacing the "As" terms with "=" and I have also tried

 Dim CmdDict As Dictionary(Of String, String) = New Dictionary(Of String, String)

For both dictionaries but this didn't work. I have tried everything that I can think of / find please help.

Could it have something to do with it being a public dictionary? I saw someone else using a public dicitonary and they're solution was to add new which I new I had to do otherwise no object would be created.

EDIT:

I just tried a regular dictionary as a public variable and it failed as well. So it appears to have nothing to do with being Nested. I will continue looking into the public object issue.

EDIT 2:

Just Tried adding Dict1 to Public Class Still not working.

Public Class GlobalVariables
    Public Shared Dict1 As Dictionary(Of Integer, Dictionary(Of String, String)) = New Dictionary(Of Integer, Dictionary(Of String, String))
End Class

After doing this Dict1 was updated to GlobalVariables.Dict1

EDIT 3:

This code is within a VSTO Addin that I am creating based on macros that I wrote in Outlook VBA. I am using Visual Studio Community 2015. The location of the code is as follows

Edit 4: Duplicate? I agree I thought that this was a duplicate answer as well but I couldn't anywhere else where the answer was to move the Create Object from Public to within a Sub for a public variable. But the rest of the question is very similar to others agreed.

Gibbs314
  • 35
  • 10
  • 1
    That code runs just fine... where exactly are you getting the exception.... what line... what does the stack trace say? – Trevor_G Feb 24 '17 at 19:18
  • The line that the error throws is at Dict1.Add(key, CmdDict) Not sure if it changes things but this is being run inside a Outlook Addin created inside VSTO – Gibbs314 Feb 24 '17 at 19:33
  • That implies the very first line is not being executed... WHere do you have this code located? – Trevor_G Feb 24 '17 at 19:35
  • All of the code is located within a VB file within my VSTO project. Within the VB file the code is in a public module. Option explicit is on. As far as level that the VB file is located at it is directly under my project CustomFiling/VBFILE.VB . When I added the global Class I did so under CustomFiling/Outlook/ThisAddin.VB – Gibbs314 Feb 24 '17 at 19:40
  • ARe you sure that file is really a module... . – Trevor_G Feb 24 '17 at 19:52
  • Your below code option worked. Thanks for your help. i am still not sure why it wasn't creating the object at the public level. and yes I ma sure it is within a module. Thanks again. – Gibbs314 Feb 24 '17 at 20:13

1 Answers1

1

Try it this way.

Public Dict1 as Dictionary(Of Integer, Dictionary(Of String, String))
Sub CreateDict(Length As Integer)
    Dict1 = New Dictionary(Of Integer, Dictionary(Of String, String)) 
    <Rest of your code> 
End Sub
Trevor_G
  • 1,331
  • 1
  • 8
  • 17
  • I tried to give you a +1 because this answered my problem but I just joined and I don't have enough rep yet. Thanks Again. – Gibbs314 Feb 24 '17 at 20:14
  • JUst click the checkmark beneath it..if u see it – Trevor_G Feb 24 '17 at 20:14
  • I'm still concerned the scope of that DIct1 may be off somehow though. Be interesting to see if it looks initialized from where you reference it. – Trevor_G Feb 24 '17 at 20:15
  • I am curious about that too. I was planning to look into this just haven't quite gotten there as I have more errors in my code now that this functions. – Gibbs314 Feb 24 '17 at 20:16
  • I just called it from a sub separate from where it was created and the value was there so I think I am all set. – Gibbs314 Feb 24 '17 at 20:23
  • Cool... that's a better way to initialize anyway... If you ever need to recreate the dictionary again later.. it's all set to do-over. – Trevor_G Feb 24 '17 at 20:25