troubleshooting Question reading CSV file - null exception

Avatar of mehmast
mehmast asked on
.NET ProgrammingVisual Basic.NET
6 Comments2 Solutions835 ViewsLast Modified:
Hello Experts
I am trying to parse CSV file which has 2 rows of data, its reading the first line correctly, storing in the structure variable but when I loop it through the second time, it errors when I try to read the first column (fields(0))

"Object reference not set to an instance of an object."

please can someone advice what is wrong with this code?

  Private Function ParseCSVs() As Boolean

        Dim LocalFilePath As String = String.Empty

        For Each aCSV As xCSV In _CSVs
            LocalFilePath = My.Settings.Item("GRFiles") & "DailyOrders\" & aCSV.CSVFileName
            Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(LocalFilePath)

                MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
                MyReader.Delimiters = New String() {","}  '{vbTab}
                Dim currentRow As String()
                'Loop through all of the fields in the file.  
                'If any lines are corrupt, report an error and continue parsing.  
                Dim OrderRequest As New cCSVOrderRequest
                OrderRequest.Items = New List(Of cCSVProductItem)

                Dim Product As ProductClass = Nothing
                Dim Item As cCSVProductItem = Nothing
                While Not MyReader.EndOfData

                        currentRow = MyReader.ReadFields()
                        ' Include code here to handle the row. 
                        Dim fields As String() = MyReader.ReadFields()
                        Item = New cCSVProductItem()
                        Item.OrderItemNumber = fields(0)
                        Item.PartNumber = fields(2)
                        Item.Description = fields(3)
                        Item.Quantity = fields(4)
                        Item.ShipTitle = fields(17)
                        Item.ShipToName = fields(18)
                        Item.ShipAddress1 = fields(19)
                        Item.ShipAddress2 = fields(20)
                        Item.ShipAddress3 = fields(21)
                        Item.ShipAddress4 = fields(22)
                        If fields(23).Contains("GB") Then
                            Item.ShipCountry = "238"
                            Item.ShipCountry = ""
                        End If
                        Item.ShipPostCode = fields(24)
                        Item.PhoneNumber = fields(28)
                    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                        Return False
                    End Try
                End While
                aCSV.OrderRequest = OrderRequest
            End Using
        Return True
    End Function
Join our community to see this answer!
Unlock 2 Answers and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros