svdotbe
svdotbe

Reputation: 168

Null Reference Exception - Read from CSV

I have to code a WPF application for college which reads from a csv file. I get a null reference exception when I want to output the parts of the CSV lines into arrays. You can find the line where the error happens in commentary. Here is the code.

Imports System.Windows.Forms
Imports System.IO
Imports System.Globalization

Class MainWindow
Private foldername As String
Private arrGemeenten As String()
Private arrOppervlakte As Double()
Private arrInwoners As Integer()
Private arrDeelgemeenten As Integer()


Private Sub cboProvincie_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles cboProvincie.SelectionChanged
    CSVInlezen()
End Sub

Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded


    FileBrowserAanmaken()
    comboBoxVullen()

End Sub



Private Sub comboBoxVullen()
    For Each file As String In IO.Directory.GetFiles(foldername)

        If file.EndsWith(".csv") Then
            Dim filenaam As String = System.IO.Path.GetFileNameWithoutExtension(file)
            cboProvincie.Items.Add(filenaam)
        End If

    Next
End Sub

Private Sub FileBrowserAanmaken()
    'folderbrowserdialog aanmaken
    Dim fbd As New FolderBrowserDialog

    fbd.SelectedPath = AppDomain.CurrentDomain.BaseDirectory


    ' Show the FolderBrowserDialog. 

    Dim result As DialogResult = fbd.ShowDialog()

    If (result = Forms.DialogResult.OK) Then
        foldername = fbd.SelectedPath
    End If
End Sub



Private Sub CSVInlezen()
    Dim filepath As String = foldername & "\" & cboProvincie.SelectedValue & ".csv"
    If File.Exists(filepath) Then
        fileInlezenHulpMethode(filepath)
    End If
End Sub

Private Sub fileInlezenHulpMethode(ByVal path As String)
    'declarations
    Dim sr As New StreamReader(path)
    Dim iTeller As Integer = 0
    Dim arrLijn As String()
    Dim culture As New System.Globalization.CultureInfo("nl-BE")

    'eerste lijn meteen uitlezen, dit zijn kolomkoppen en hebben we niet nodig
    'read out first line, these are titles and we don't need them
    sr.ReadLine()

    Do While sr.Peek <> -1
        Dim lijn As String = sr.ReadLine()
        arrLijn = lijn.Split(";")
        arrGemeenten(iTeller) = Convert.ToString(arrLijn(0)) 'HERE I GET THE ERROR!
        arrOppervlakte(iTeller) = Double.Parse(arrLijn(2), NumberStyles.AllowDecimalPoint, culture.NumberFormat)
        arrInwoners(iTeller) = Integer.Parse(arrLijn(3), NumberStyles.Integer Or NumberStyles.AllowThousands, culture.NumberFormat)
        arrDeelgemeenten(iTeller) = Convert.ToString(arrLijn(4))
    Loop
End Sub

End Class

Upvotes: 0

Views: 572

Answers (1)

Guffa
Guffa

Reputation: 700870

You haven't created the array, you have only created a reference for it. To create the array you need to specify a size, for example:

Private arrGemeenten As String(100)

However, to specify the size, you need to know the size when you create the array. (Well, actually you put all data in the first item, so just the size 1 would keep it from crashing, but I don't thing that's what you intended.) You probably want to use lists instead:

Private gemeenten As New List(Of String)()

Then you use the Add method to add items to the list:

gemeenten.Add(Convert.ToString(arrLijn(0)))

Also, consider putting the data in a single list of a custom object, instead of having several lists of loosely coupled data.

Upvotes: 1

Related Questions