Stream Reading/Parsing/Array issue

N

Necromis

The below code is giving me an error of.....
"System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."

......in this line........

the yellow error highlight is highlighting the line below. (arrow)

Do
MA = Parse(streamLine)
MyArray(Counter).Acctno = MA(0) <====Highlighted row

Can anyone suggest as to what is causing the problem and a solution?
Or possibly an alternate way of acomplishing what the code is trying
to do. Thanks

Imports System.IO
Imports Microsoft.VisualBasic
Public Class Form1
Structure MyType
Dim Acctno As String
Dim Name As String
Dim expdate As String
Dim cr_line As String
End Structure
Dim MyArray As MyType()

Private Function Parse(ByRef theLine As String) As String()

Dim tempArray() As String
Dim delimiters As Char = (","c)
tempArray = theLine.Split(delimiters)

Return tempArray
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim infile As String
Dim fileLoc As String
fileLoc = "F:\VB Express\"
infile = fileLoc & infileBox.Text
Me.LoadTextFileIntoMyArray(infile)
End Sub

Private Sub LoadTextFileIntoMyArray(ByVal filePath As String)

Dim reader As New StreamReader(filePath, FileMode.Open)

Dim streamLine As String = reader.ReadLine()

Dim MA As String()
Dim Counter As Integer = 0

Do
MA = Parse(streamLine)
MyArray(Counter).Acctno = MA(0)
MyArray(Counter).Name = MA(1)
MyArray(Counter).expdate = MA(2)
MyArray(Counter).cr_line = MA(3)

Counter += 1

Loop While Not theLine Is Nothing
reader.Close()

End Sub
End Class
 
A

AlexS

I miss MyArray initialization with New
Did you create instance before using it as assignment target?

HTH
Alex
 
B

Brian Gideon

The below code is giving me an error of.....
"System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."

.....in this line........

the yellow error highlight is highlighting the line below. (arrow)

Do
MA = Parse(streamLine)
MyArray(Counter).Acctno = MA(0) <====Highlighted row

Allow the debugger to break when the exception occurs and pull up the
locals or watch window. First, check MyArray to see if it is null.
Then, check the slot MyArray(Counter) to see if it is null.

Brian
 
C

Cor Ligthert [MVP]

Necromis,

Don't forget beside the given advices to set Option Strict to On.

Cor
 

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

Similar Threads


Top