Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I get an error when i am trying to put an item from one array into another array. I have a tab-delimited text file that has about 100 records with 6 fields for each record and what i am doing is error checking the employee id field for each record to see if it is 5 digits/characters long. I amdoing this by putting the whole file into a string and using the split function to separate the records and then putting that string into an array and then spliting each each into fields. at this point i am checking to see if the employee id field(which is key 4) is 5 digits long. If it is then i want to put that record into a "good" array and if it is not 5 digits then i want to put it into a "bad" array. If anyone can help me i would be very gratful. or if anyone knows a better way to do this please let me know. Thank you for your time!!!!

 

Object reference not set to an instance of an object.

 

Source Error:

 

 

Line 215: i += 1

Line 216: Else

Line 217: sGood(j) = sTemp(a) <---This is my problem

Line 218: 'sGood(j) = strItems

Line 219: j += 1

 

 

--->this is the code for the entire function<---

 

Private Function OpenFiletest(ByVal strPath As String) As Boolean

Dim sr As StreamReader

sr = System.IO.File.OpenText(strPath)

Dim i As Integer = 0

Dim j As Integer = 0

Dim sTemp() As String

Dim sTempFields() As String

Dim strItems As String

Dim sGood() As String

Dim sBad() As String

'loop through the text file

While sr.Peek <> -1

strItems = sr.ReadLine()

sTemp = strItems.Split(vbCrLf)

End While

Dim a As Integer

For a = 0 To UBound(sTemp)

sTempFields = sTemp(a).Split(vbTab)

If Len(sTempFields(4)) <> 5 Then

sBad(i) = sTemp(a)

i += 1

Else

sGood(j) = sTemp(a)

j += 1

End If

Next

sr.Close()

For i = 0 To UBound(sBad)

lblSplittext.Text = (sBad(i).ToString & vbCrLf)

Next

For j = 0 To UBound(sGood)

lblPlainText.Text = (sGood(j).ToString & vbCrLf)

Next

Return True 'file was there...with no errors

End Function

  • Administrators
Posted

When you do

Dim sGood() As String
Dim sBad() As String

you are declaring the arrays but not their size you will either need to set them to the correct size initial i.e.

Dim sGood(100) As String
Dim sBad(100) As String

 

or use Redim Preserve to resize the array at runtime.

 

Alternatively you may want to consider something like an ArrayList as this removes the need to manage the size explicitly.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

thanks alot! i finally figured everything out!!

 

When you do

Dim sGood() As String
Dim sBad() As String

you are declaring the arrays but not their size you will either need to set them to the correct size initial i.e.

Dim sGood(100) As String
Dim sBad(100) As String

 

or use Redim Preserve to resize the array at runtime.

 

Alternatively you may want to consider something like an ArrayList as this removes the need to manage the size explicitly.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...