-4
Imports System.IO

Module Module1
Structure TownType
    Dim Name As String
    Dim County As String
    Dim Population As String
    Dim Area As String
End Structure

Dim reader As StreamReader
Dim writer As StreamWriter

Sub Main()
    Dim FileName As String = "C:\Users\Desktop\Towns.csv"
    reader = New StreamReader(FileName)
    Dim Count As Integer
    Dim Line As String
    Dim TownList() As TownType
    Dim MyFormat As String = "{0,  -22} {1,  -16} {2,  -8} {3,  -8}"

    Do Until reader.EndOfStream = True
        Line = reader.ReadLine
        Dim record = Line.Split(",")
        TownList(Count).Name = record(0)
        TownList(Count).County = record(1)
        TownList(Count).Population = record(2)
        TownList(Count).Area = record(3)
        Console.WriteLine(String.Format(MyFormat, TownList(Count).Name, TownList(Count).County, TownList(Count).Population, TownList(Count).Area))
        Count = Count + 1
    Loop
    Console.ReadLine()
End Sub

End Module

I am trying to read from the contents from the file and display them in a table based format on the console however with this code on the line 'TownList(Count).Name = record(0) ' I get the error NullReferenceExceptionErrorWasUnhandled and I don't know why?

John Saunders
  • 157,405
  • 24
  • 229
  • 388
Todd432
  • 69
  • 1
  • 5
  • 9
  • 2
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Nov 22 '14 at 22:47

1 Answers1

1

You have to initialize the array first. For example:

Dim TownList(10) As TownType

Then you have to initialize each TownList:

Do Until reader.EndOfStream = True
    Line = reader.ReadLine
    Dim record = Line.Split(",")
    Dim tt As New TownType()
    tt.Name = record(0)
    ' .... '
    TownList(Count) = tt 
    ' .....'

But since you don't know the final size of the array you should use a List(Of TownType) instead.

Dim TownList As New List(Of TownType)
Do Until reader.EndOfStream = True
    Line = reader.ReadLine
    Dim record = Line.Split(",")
    Dim tt As New TownType()
    tt.Name = record(0)
    ' .... '
    TownList.Add(tt)
    ' .....'

If you need an array you can use TownList.ToArray at the end.

Tim Schmelter
  • 411,418
  • 61
  • 614
  • 859
  • how would I get it so that on each repitition of the loop it goes t1,t2,t3... etc? – Todd432 Nov 22 '14 at 23:02
  • @Todd432: i don't understand the comment. What is `t1`, `t2`, `t3`? With the list-approach you don't need to use a `count`-variable. – Tim Schmelter Nov 22 '14 at 23:09
  • So how do you reference a specific line of the list, for example if I wanted to do a search within the list? – Todd432 Nov 22 '14 at 23:14
  • That's another question - it seems you have an answer for the current question. – OneFineDay Nov 22 '14 at 23:18
  • @Todd432: you can access an item in a list in the same as in an array: via indexer. So for example: `Dim t3 As TownType = TownList(2)`. Internally a list also uses an array to store the items. But it can be resized as opposed to an array which has a fixed size. – Tim Schmelter Nov 22 '14 at 23:21