kevjett Posted July 14, 2004 Posted July 14, 2004 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 Quote
Administrators PlausiblyDamp Posted July 14, 2004 Administrators Posted July 14, 2004 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
kevjett Posted July 14, 2004 Author Posted July 14, 2004 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.