0

Can you spot my error?

I'm pretty new at VB.net, so I'm sure it's something obvious... I've read all the references to VB.net nullreferenceexception here on StackOverflow and am still confused.

Here's the code:

    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

the fifth line is failing:

        strDisplayVerse = "Verse " & strDisplayVerse.Substring(0, 1) &
            ", slide " & strDisplayVerse.Substring(2, 1)

The second loop is code that works. I swapped the line above this code to see if maybe the array setting was causing the problem - I received the error on the same code line. I then swapped the code with:

        TxtBxVerses.Text = TxtBxVerses.Text & strDisplayVerse & Environment.NewLine

which also works in the second loop - same error on this line of code.

I can see that strDisplayVerse is equal to "1-1 blah blah blah", so this variable has data and is not null. arrVersesChorusOther is just an undefined size string array defined with PUBLIC just after the Public Class Form1 statement. intVCO is a PUBLIC integer set just below arrVersesChorusOther. TxtBxVerses is a textbox on the form.

LEBoyd
  • 151
  • 12
  • I looked that up before I posted the question. Unfortunately, I could not see a solution in that post. – LEBoyd Jul 16 '18 at 15:49
  • 1
    In a select few cases the debugger can select the wrong statement when an exception is thrown. You'd need to consider the statement before it as well. Which makes "an undefined size string array" an excellent candidate, that array must have a well defined size. – Hans Passant Jul 16 '18 at 16:01

1 Answers1

0

I've been playing around with the code to try to figure out what the problem is.

"arrVersesChorusOther is just an undefined size string array defined with PUBLIC" is the culprit.

Being new to VB.Net, I did not know you cannot define a Public array without a dimension. I added a size to the array and it now works.

I do not know if you can have a "Dim arrABC() as String" in a subroutine, but I guess I'll figure that one out when I need it.

LEBoyd
  • 151
  • 12
  • If you don't want to specify an exact size for your arrays you can use a [**`List(Of T)`**](https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx) instead. Every time you want to append a new item to it you call its [**`Add()` method**](https://msdn.microsoft.com/en-us/library/3wcytfd1(v=vs.110).aspx), instead of specifying an index. – Visual Vincent Jul 17 '18 at 10:36
  • Thanks for pointer, @Visual Vincent! That's very good to know. – LEBoyd Jul 17 '18 at 16:37