Avatar of mehmast
mehmast

asked on 

vb.net reading CSV file - null exception

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
                    Try

                        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"
                        Else
                            Item.ShipCountry = ""
                            'CommonFunctions.SendErrorEmail("")
                        End If
                        Item.ShipPostCode = fields(24)
                        Item.PhoneNumber = fields(28)
                        OrderRequest.Items.Add(Item)
                    Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                        'CommonFunctions.SendErrorEmail("")
                        Return False
                    End Try
                End While
                aCSV.OrderRequest = OrderRequest
            End Using
        Next
        Return True
    End Function

Open in new window

Untitled.png
Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
Fernando Soto
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

That is because you are reading both rows in the first pass and therefore when you try to get the next row there is no more data, fields(0) is null and you are trying to assign that to a variable in this line of code.

 Item.OrderItemNumber = fields(0)

This is where you are reading in both lines in the file in the first pass. Modify the code to read once per pass.

Try
    '' First time you read the line from the file
    currentRow = MyReader.ReadFields()
    ' Include code here to handle the row. 
    
    '' Now you fread the second line in from the file 
    Dim fields As String() = MyReader.ReadFields()

Open in new window

Avatar of mehmast
mehmast

ASKER

Hello
thank you for picking this up for me
i have headers in first row, so this line will ignore the headers
currentRow = MyReader.ReadFields()

the second and the third rows has data which I want to extract,

how can i make sure i only run the headers once in the loop? please advice
SOLUTION
Avatar of john hill
john hill

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of mehmast
mehmast

ASKER

thank you
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Not a problem mehmast, glad to help.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo