0

So I am creating a program that needs to be able to read line by line from a .cfg (config) file, it can open it happily; here is the code:

    OpenConfig.ShowDialog()
    file = OpenConfig.FileName()
    fileReader()

However when it tries to read the file, using this code:

Function fileReader()
    Dim reader As New StreamReader(file)
    Dim vLb As ListBox = shopTabs.SelectedTab.Controls.Item(10) 'Listbox Variable
    For i = 0 To reader.Peek

        textline(i) = reader.ReadLine()
        vLb.Items.Add(i)

    Next

    Return True
End Function

It throws an exception at the line:

textline(i) = reader.ReadLine()

Any help would be greatly appreciated as I can't work out why it does so.

Jeffrey Kemp
  • 59,135
  • 14
  • 106
  • 158
  • Have you checked if `reader is NOT Nothing`? – Hauns TM Mar 03 '15 at 20:54
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – James Thorpe Mar 03 '15 at 20:56
  • As in: If Not file Is Nothing Then ? I just added it and it still passes through into the reader. Also, James: It's a different programming language. – Laurie Walpole Mar 03 '15 at 20:57
  • @LaurieWalpole The duplicate is tagged both c# and vb.net - the troubleshooting techniques apply equally well to both. – James Thorpe Mar 03 '15 at 20:58
  • You are going to get in trouble with this code. What if the user clicks "Cancel" on the OpenConfig form? Your code still runs. Referencing a control by an index number is also dangerous and difficult to maintain in the future: `shopTabs.SelectedTab.Controls.Item(10)`. You probably want to use `File.ReadAllLines` instead of you reader and Peek method. – LarsTech Mar 03 '15 at 21:03
  • 1
    Also: there is a [VB-based answer](http://stackoverflow.com/a/26761773/1070452) attached to the NRE question. In fact there are 18 or so answers – Ňɏssa Pøngjǣrdenlarp Mar 04 '15 at 01:50

1 Answers1

0

Your code could be simplified into the following code:

Using openConfig As New OpenFileDialog()
  If openConfig.ShowDialog(Me) = DialogResult.OK Then
    For Each s As String In File.ReadAllLines(openConfig.FileName)
      ListBox1.Items.Add(s)
    Next
  End If
End Using

As I commented, your code does some things that are highly questionable and undoubtedly difficult to maintain, such as referencing a control by the index property.

I suspect that your project would benefit from using UserControls too, since I'm guessing you have the same controls placed in every tab (a ListBox is always control index #10?).

LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • The reason I have to reference things by Index numbers is due to the fact that EVERYTHING inside of my TabControl is dynamically created during runtime. I would paste the code here, but basically I had to define every item that goes inside the tab controls, set the location, the sizing, name etc... To me referencing them as an Index made much more sense especially as on each Tab there needs to be different content on every tab page and yes, the ListBox is index 10, every time. – Laurie Walpole Mar 04 '15 at 13:05