Results 1 to 3 of 3

Thread: [RESOLVED] Add Text to Array

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    2

    Resolved [RESOLVED] Add Text to Array

    Hello, I am having an issue with my coding. I believe that this is correct, but when I try to load the form it results in an error. I am trying to load each individual line of the Cat1.txt file into the wordsarray that I have declared publicly because I will use the array in other subs. When I try this, I get a runtime error that states "NullReferenceException was unhandled." Is there a better way to code this? Also, how could I exclude the first line of the text file from the array?

    Code:
    Public Class MainForm
    
    Dim wordsarray() As String
    
    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
     Dim inFile As IO.StreamReader
            Dim strInfo As String
            Dim intSubscript As Integer = 0
    
            If IO.File.Exists("Cat1.txt") Then
                inFile = IO.File.OpenText("Cat1.txt")
                Do Until inFile.Peek = -1
                    strInfo = inFile.ReadLine
                    wordsarray(intSubscript) = strInfo
                    intSubscript = intSubscript + 1
                Loop
                inFile.Close()
            Else
                MessageBox.Show("Can't find the Cat1.txt file", "Category 1 File Missing", MessageBoxButtons.OK, MessageBoxIcon.Information)
    
            End If
    
    End Sub
    
    End Class
    Any help would be appreciated.

  2. #2
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Re: Add Text to Array

    Check this code.

    Code:
    Public Class Form1
        Dim wordsarray As New System.Collections.ArrayList
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim strInfo As String
            
            If IO.File.Exists("Cat1.txt ") Then
    
                Dim sr As New System.IO.StreamReader("Cat1.txt ")
                strInfo = sr.ReadLine
                While sr.Peek <> -1
                    strInfo = sr.ReadLine
                    wordsarray.Add(strInfo)
                End While
    
            Else
                MessageBox.Show("Can't find the Cat1.txt file", "Category 1 File Missing", MessageBoxButtons.OK, MessageBoxIcon.Information)
    
            End If
    
        End Sub
    End Class
    thanks
    amrita

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    2

    Re: [RESOLVED] Add Text to Array

    Thank you so much, that worked perfectly!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width