Adding a Structure to a List(Of T) in another Structure




I am sort of a newbie that needs help. Listed below is the code for my
entire Class. In the procedure "LoadRecDetail" (at the end of the
class) I get an exception error and I just do not see what I have done
wrong. I have listed the entire class in case I did something wrong
prior to the procedure. Note that field names have been made generic
but identical type to the actual field names.

Any help will be greatly appreciated as well as general comments.
Imports System.IO
Imports System.Collections
Public Class clsCSVProcs

Public Structure TableDef
Dim RecHedrFld1 As String
Dim RecHedrFld2 As String
Dim RecHedrFld3 As String
Dim RecHedrFld4 As String
Dim RecHedrFld5 As String
Dim RecHedrFld6 As String
Dim DetailsInfo As List(Of DetailDef)
End Structure

Public Structure DetailDef
Dim RecDetailFld1 As Integer
Dim RecDetailFld2 As String
Dim RecDetailFld3 As String
Dim RecDetailFld4 As String
Dim RecDetailFld5 As String
Dim RecDetailFld6 As String
Dim RecDetailFld7 As String
End Structure

Public Function ConvertCSV(ByVal CSVFileName As String, _
ByRef CSVRecList As
List(Of TableDef)) As Boolean
Dim cIoProcs As New clsCommonIOProcs
Dim CurrentRecord As New TableDef

Dim ErrorCode As Integer = 0
Dim ErrorText As String = ""

Dim RecKey As String = ""
Dim CurrentKey As String
Dim CSVFields() As String
Dim ErrorMsg As String

If Not cIoProcs.ValidFileName(CSVFileName, ErrorCode,
ErrorText) Then
ErrorMsg = "IO Error - " & ErrorCode.ToString & _
vbCrLf & vbCrLf & _
MsgBox(ErrorMsg, vbCritical + vbCancel, "CSV Conversion")
Return False
End If

If (CSVRecList.Count > 0) Then CSVRecList.Clear()

Using CSVReader As New FileIO.TextFieldParser(CSVFileName)
With CSVReader
.TextFieldType = FileIO.FieldType.Delimited
End With

While Not CSVReader.EndOfData
CSVFields = CSVReader.ReadFields()
CurrentKey = CSVFields(0)

If (CurrentKey <> "") Then ' Ignore CSV Blank
Select Case String.Compare(CurrentKey, RecKey,
Case -1, 1
' New Record, If Not First Flush
Current CSV Record
If (RecKey <> "") Then

' initialize new CSV Record by Loading
Header data
Call LoadRecHeader(CSVFields,
CurrentRecord, CurrentKey)
Call LoadRecDetail(CSVFields,
Case 0
' Same CSV Record, Load Record Details
Call LoadRecDetails(CSVFields,
End Select
End If
Catch ex As FileIO.MalformedLineException
Return False
End Try
End While

' End of CSV File, Flush Current CSV Record
End Using

Return True
End Function

Private Sub LoadRecHeader(ByVal CSVFields() As String, _
ByRef CurrentRecord As
CDTableDef, _
ByRef CurrentKey As
' Fields
' 0 RecHedrFld1
' 1 RecHedrFld2
' 2 - Not Used -
' 3 RecHedrFld3
' 4 RecHedrFld4
' 5 RecHedrFld5

With CurrentRecord
.RecHedrFld1 = CSVFields(0)
.RecHedrFld2 = CSVFields(1)
.RecHedrFld3 = CSVFields(3)
.RecHedrFld4= CSVFields(4)
.RecHedrFld5 = CSVFields(5)

CurrentKey = .RecHedrFld1
If IsNothing(.DetailsInfo) Then Return
End With
End Sub

Private Sub LoadRecDetail(ByVal CSVFields() As String, _
ByRef CurrentRecord As
' Fields
' 6 RecDetailFld1
' 7 RecDetailFld2
' 8 RecDetailFld3
' 9 RecDetailFld4
' 10 RecDetailFld5
' RecDetailFld6 ' additional field
' 11 RecDetailFld7

Dim RecDetail As DetailDef

With CDTrack
.RecDetailFld1 = CSVFields(6)
.RecDetailFld2 = CSVFields(7)
.RecDetailFld3 = CSVFields(8)
.RecDetailFld4 = CSVFields(9)
.RecDetailFld5 = CSVFields(10)
.RecDetailFld6 = ""
.RecDetailFld7 = CSVFields(11)
End With

|NullReferenceException was unhandled
CurrentRecord.DetailsInfo.Add(RecDetail) <----|
End Sub |
Object reference not set to instance of object
End Class
Jerry B


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question