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?