2017 VB.net nullreferenceexception

2017 VB.net nullreferenceexception,vb.net,Vb.net,你能发现我的错误吗 我是VB.net的新手,所以我确信这是显而易见的。。。我已经阅读了StackOverflow上对VB.net nullreferenceexception的所有引用,仍然感到困惑 代码如下: For Each oSlide In oPres.Slides.Range strDisplayVerse = oSlide.Shapes.Title.TextFrame.TextRange.Characters.Text If (IsNumeri

你能发现我的错误吗

我是VB.net的新手,所以我确信这是显而易见的。。。我已经阅读了StackOverflow上对VB.net nullreferenceexception的所有引用,仍然感到困惑

代码如下:

    For Each oSlide In oPres.Slides.Range
        strDisplayVerse = oSlide.Shapes.Title.TextFrame.TextRange.Characters.Text
        If (IsNumeric(strDisplayVerse.Substring(0, 1)) = True And strDisplayVerse.Substring(2, 1) = "1") Then
            arrVersesChorusOther(intVCO) = "Verse " & strDisplayVerse.Substring(0, 1)
            strDisplayVerse = "Verse " & strDisplayVerse.Substring(0, 1) &
                ", slide " & strDisplayVerse.Substring(2, 1)
            intVCO = intVCO + 1
        End If
    Next
'-----------
    For Each oSlide In oPres.Slides.Range
        strDisplayVerse = oSlide.Shapes.Title.TextFrame.TextRange.Characters.Text
        If strDisplayVerse.Substring(0, 1) = "C" Then
            strDisplayVerse = "Chorus, slide " & strDisplayVerse.Substring(2, 1)
        Else
            If IsNumeric(strDisplayVerse.Substring(0, 1)) Then
                strDisplayVerse = "Verse " & strDisplayVerse.Substring(0, 1) &
                    ", slide " & strDisplayVerse.Substring(2, 1)
            End If
        End If
        TxtBxVerses.Text = TxtBxVerses.Text & strDisplayVerse & Environment.NewLine
    Next
第五行失败了:

        strDisplayVerse = "Verse " & strDisplayVerse.Substring(0, 1) &
            ", slide " & strDisplayVerse.Substring(2, 1)
第二个循环是有效的代码。我交换了这段代码上面的一行,看看是否是数组设置导致了问题-我在同一代码行收到了错误。然后,我将代码替换为:

        TxtBxVerses.Text = TxtBxVerses.Text & strDisplayVerse & Environment.NewLine
这也适用于第二个循环-在这行代码中出现相同的错误


我可以看到strDisplayVerse等于“1-1废话废话”,所以这个变量有数据并且不是空的。arrVersesChorusOther只是一个未定义大小的字符串数组,它是在PUBLIC Class Form1语句之后用PUBLIC定义的。intVCO是一个公共整数,设置在ArrverseChorusOther的正下方。TxtBxVerses是表单上的一个文本框

我一直在玩弄代码,试图找出问题所在

“arrVersesChorusOther只是一个用PUBLIC定义的未定义大小的字符串数组”是罪魁祸首

作为VB.Net的新手,我不知道没有维度就不能定义公共数组。我在数组中添加了一个大小,现在可以使用了


我不知道是否可以在子例程中使用“Dim arrABC()as String”,但我想我会在需要时找到它。

我在发布问题之前查阅了它。不幸的是,我在那篇文章中看不到解决方案。在少数情况下,当抛出异常时,调试器可能会选择错误的语句。你也需要考虑之前的声明。这使得“未定义大小的字符串数组”成为一个很好的候选数组,该数组必须有一个定义良好的大小。如果不想为数组指定确切的大小,则可以使用。每次你想给它添加一个新的项目,你就调用它,而不是指定一个索引。谢谢你的指针,@Visual文森特!很高兴知道。