-1

I'm making a tool that reads a file into a string array and then edits the text of each array index before displaying all the lines of the string array in a RichTextbox control.

Here is my code so far:

Private Sub btnOpen_Click(sender As Object, e As EventArgs) Handles btnOpen.Click
    Dim ofd As New OpenFileDialog
    ofd.FileName = ""
    ofd.Title = "Open File"
    If ofd.ShowDialog = DialogResult.OK Then
        'read file
        Dim reader As New StreamReader(ofd.FileName, Encoding.Default)
        Dim line As String = Nothing : Dim counter As Integer = 0
        Do
            line = reader.ReadLine
            readLines(counter) = line
            counter += 1
        Loop Until line Is Nothing
        reader.Close() : counter = 0

        'removing text from left
        While counter < readLines.Length
            readLines(counter) = readLines(counter).Substring(0, readLines(counter).Length - txtLimit.Text)
            counter += 1
        End While
        counter = 0

        'show result in RichTextBox
        While counter < readLines.Length
            rtBox.Text = rtBox.Text + readLines(counter) + vbNewLine
            counter += 1
        End While

    ElseIf ofd.ShowDialog = DialogResult.Cancel Then
        MsgBox("Operation Cancelled!")
    Else
        MsgBox("Unable to open file!")
    End If
End Sub

Right below readLines(counter) = line, where it says counter += 1, I get this error:

System.NullReferenceException: Object reference not set to instance of Object

What am I doing wrong?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • btw, I'm using .net framework 4.7.1 <<>> The string array is initialized as: Dim readLines() As String – SimpleCoder Apr 26 '18 at 13:10
  • The information in the image should be in the text of the question. We would need to know how readLines is initialized. I would suggest a list instead. – the_lotus Apr 26 '18 at 13:11
  • Thanks @the_lotus – SimpleCoder Apr 26 '18 at 13:13
  • 3
    Welcome to Stack Overflow! Images and screenshots can be a nice addition to a post, but please make sure the post is still clear and useful without them. **Don't post images of code or error messages.** Read [why](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557). Instead copy and paste or type the actual code/message into the post directly. – Filnor Apr 26 '18 at 13:14
  • 3
    Stop using `:`. There is no good reason to use it, and it messes with the debugger. The line that is actually causing the exception is the line above the highlighted one. – A Friend Apr 26 '18 at 13:35
  • The colon (`:`) can come in handy when shortening certain (unnecessary?) statements, however it should not be used as a way of shortening lines that are actually doing something important. As @AFriend said it messes with the debugger (or more correctly: it messes with _you_) which makes it harder to identify which line threw an exception. – Visual Vincent Apr 26 '18 at 15:01
  • The punchline is that you shouldn't use it when two or more of the shortened lines may throw an exception. However you can use it to shorten certain unnecessary statements, for instance an empty `Try/Catch` statement: `Try : DoSomething() : Catch : End Try` - where you don't care if an exception is thrown or not. – Visual Vincent Apr 26 '18 at 15:02
  • thanks a lot. i will remember that – SimpleCoder Apr 27 '18 at 05:54

1 Answers1

0

I suspect the problem with the original is the readLines array is not initialized properly. The code tries to write past the end of the array, and the debugger is for some reason showing the problem on the wrong line.

It's not enough to Dim an array. That just sets up a reference variable, but that variable is still null/Nothing. You must also create the New (hint) array object.

That said, this function can be reduced quite a bit.

Private Sub btnOpen_Click(sender As Object, e As EventArgs) Handles btnOpen.Click
    Dim ofd As New OpenFileDialog
    ofd.Title = "Open File"
    Dim result As DialogResult = ofd.ShowDialog()

    If result = DialogResult.Cancel Then
        MsgBox("Operation Cancelled!")
        Exit Sub
    ElseIf result <> DialogResult.Ok
        MsgBox("Unable to open file!")
        Exit Sub
    End If

    Dim lines =  File.ReadLines(ofd.FileName).Select(Function(line) line.Substring(0, line.Length - CInt(txtLimit.Text)) & vbNewLine)

    For Each line As String In Lines
         rtBox.Text &= line
    Next
End Sub
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 1
    Assuming that the `RichTextBox` is initially empty, that last loop should be replaced with `rtBox.Lines = lines.ToArray()`. – jmcilhinney Apr 26 '18 at 13:35
  • I thought of that, but I wanted to resemble the original code at least a little in this part of the function. – Joel Coehoorn Apr 26 '18 at 13:37