2012-09-29 79 views
0

我必須編寫一個從csv文件讀取大學的WPF應用程序。當我想將CSV行的部分輸出到數組時,我得到一個空引用異常。你可以在評論中找到錯誤發生的地方。這是代碼。空引用異常 - 從CSV讀取

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 
+0

幾乎所有NullReferenceException的情況都是一樣的。請參閱「[什麼是.NET中的NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)」的一些提示。 –

回答

1

您尚未創建數組,您只爲其創建了參考。要創建數組,你需要指定一個大小,例如:

Private arrGemeenten As String(100) 

然而,指定大小,你需要知道的大小,當你創建數組。 (嗯,其實你把所有的數據中的第一項,所以只是大小1會保持它的崩潰,但我不認爲是你打算什麼東西。)你可能想使用列表來代替:

Private gemeenten As New List(Of String)() 

然後使用Add方法將項目添加到列表:

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

此外,考慮將在自定義對象的單個列表中的數據,而不必數據鬆耦合的若干名單。

+0

感謝您的幫助。我知道創建一個對象會更好,但是在我得到的任務的註釋中,指定它必須位於單獨的數組中。我也知道有一些像ArrayLists。你會推薦這個還是你認爲這是一個壞主意? – user1422136

+0

@ user1422136:不要使用'ArrayList',它基本上是一個List(Of Object)'。通用列表性能更好,更易於使用。 – Guffa