VB.net 中的嵌套字典 System.NullReferenceException
Nested Dictionary System.NullReferenceException in VB.net
我已在互联网上搜索以寻找解决此问题的方法,但空异常仍然存在。基本上我在尝试向嵌套字典添加值时遇到 System.NullReferenceException 错误。
这是我的代码。
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
请注意,此代码改编自我在 VBA 中针对 outlook 2016 的原始项目。我正在尝试将此代码重新改编为 outlook 插件。
我试过用“=”替换 "As" 项,我也试过
Dim CmdDict As Dictionary(Of String, String) = New Dictionary(Of String, String)
对于两个词典,但这没有用。我已经尝试了所有我能想到的/找到的东西请帮助。
这与它是一本 public 字典有关系吗?我看到其他人使用 public dicitonary,他们的解决方案是添加新的,我必须这样做,否则不会创建任何对象。
编辑:
我刚刚尝试将普通字典作为 public 变量,但它也失败了。所以它似乎与被嵌套无关。我将继续调查 public 对象问题。
编辑 2:
刚刚尝试将 Dict1 添加到 Public Class 仍然无效。
Public Class GlobalVariables
Public Shared Dict1 As Dictionary(Of Integer, Dictionary(Of String, String)) = New Dictionary(Of Integer, Dictionary(Of String, String))
End Class
执行此操作后,Dict1 已更新为 GlobalVariables.Dict1
编辑 3:
此代码位于我根据我在 Outlook VBA 中编写的宏创建的 VSTO 插件中。我正在使用 Visual Studio Community 2015。The location of the code is as follows
编辑 4:
复制?我同意我认为这也是一个重复的答案,但我不能在其他任何地方找到答案是将创建对象从 Public 移动到 public 变量的 Sub 中。但是剩下的问题和其他人同意的很相似。
这样试试。
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
我已在互联网上搜索以寻找解决此问题的方法,但空异常仍然存在。基本上我在尝试向嵌套字典添加值时遇到 System.NullReferenceException 错误。
这是我的代码。
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
请注意,此代码改编自我在 VBA 中针对 outlook 2016 的原始项目。我正在尝试将此代码重新改编为 outlook 插件。
我试过用“=”替换 "As" 项,我也试过
Dim CmdDict As Dictionary(Of String, String) = New Dictionary(Of String, String)
对于两个词典,但这没有用。我已经尝试了所有我能想到的/找到的东西请帮助。
这与它是一本 public 字典有关系吗?我看到其他人使用 public dicitonary,他们的解决方案是添加新的,我必须这样做,否则不会创建任何对象。
编辑:
我刚刚尝试将普通字典作为 public 变量,但它也失败了。所以它似乎与被嵌套无关。我将继续调查 public 对象问题。
编辑 2:
刚刚尝试将 Dict1 添加到 Public Class 仍然无效。
Public Class GlobalVariables
Public Shared Dict1 As Dictionary(Of Integer, Dictionary(Of String, String)) = New Dictionary(Of Integer, Dictionary(Of String, String))
End Class
执行此操作后,Dict1 已更新为 GlobalVariables.Dict1
编辑 3:
此代码位于我根据我在 Outlook VBA 中编写的宏创建的 VSTO 插件中。我正在使用 Visual Studio Community 2015。The location of the code is as follows
编辑 4: 复制?我同意我认为这也是一个重复的答案,但我不能在其他任何地方找到答案是将创建对象从 Public 移动到 public 变量的 Sub 中。但是剩下的问题和其他人同意的很相似。
这样试试。
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