locked
Building a string using substrings from a Stream Reader RRS feed

  • Question

  • Hello all.

    I am working on a small bit of code in VS2012 that builds a text file from an existing text file. 

    The first 404 lines(or however many the user wants) of the text file are identical, but after that, I just need to pull out certain parts from each line until the end of the file.

    The first part of the code is a Do While Loop:

    Do While linecounter <= CInt(linestart)

    If filereader.Peek() >= 0 Then   

    sb.AppendLine(filereader.ReadLine())            linecounter += 1 End If 

    Loop

    This just builds the string up to the row indicated by the user.

    After that I run

    Do Until linecounter = totalrowcount

    sb.AppendLine(filereader.ReadLine.Substring(0, 7))       

    linecounter += 1           

    Loop

    I am getting a System.NullReferenceException pointing to the substring that I'm trying to use here. What am I missing? Is there a better way to accomplish this?



    • Edited by Mellonjollie Wednesday, December 12, 2012 5:41 PM syntax
    Wednesday, December 12, 2012 5:39 PM

Answers

  • You must be trying to read past the end of the file. If you do that, ReadLine will return Nothing and SubString will get a NullReferenceException.  Be aware that you will also get an ArgumentOurOfRangeException from Substring(0, 7) if the line you read contains less than 7 characters.
    • Marked as answer by Mellonjollie Wednesday, December 12, 2012 8:35 PM
    Wednesday, December 12, 2012 7:24 PM

All replies

  • If the line isn't long enough you get an out of bounds exception.  Not sure what would cause a null exception.  Does this code give the same error:

      Sub ModifyFile(FileName As String, LineStart As Integer)
        Dim Lines() As String = File.ReadAllLines(FileName)
        For I As Integer = LineStart To Lines.Length - 1
          Lines(I) = Lines(I).Substring(0, 7)
        Next
      End Sub

    Wednesday, December 12, 2012 7:22 PM
  • You must be trying to read past the end of the file. If you do that, ReadLine will return Nothing and SubString will get a NullReferenceException.  Be aware that you will also get an ArgumentOurOfRangeException from Substring(0, 7) if the line you read contains less than 7 characters.
    • Marked as answer by Mellonjollie Wednesday, December 12, 2012 8:35 PM
    Wednesday, December 12, 2012 7:24 PM
  • Below is the proper algoritm for youir algoritm.  It is easier to understand and less prone to errors.

            Dim filereader As StreamReader
            Dim inputline As String
            Dim linestartnum As Integer = CInt(linestart)
            Dim linecounter As Integer
            Dim sb As StringBuilder
            Do While filereader.EndOfStream = False
                inputline = filereader.ReadLine()
                Select Case linecounter
                    Case Is <= linestartnum
                        sb.AppendLine(inputline)
                    Case Else
                        sb.AppendLine(inputline.Substring(0, 7))
                End Select
            Loop


    jdweng

    Wednesday, December 12, 2012 7:27 PM
  • Yes that was my first problem Blackwood. Once I ran through the file to get the total row count, I forgot to reset the reader. Joel's solution looks good, but after doing some more research I believe using a Split function will work better than a substring since the rows I need to filter through are comma-delimited. 

    At this point, I've successfully used the Split to break up comma-delimited line and place the values into an array. What I need to do now is create a different array that contains unique values from the 5th position of the first array. 

    I cobbled together

    If line <> "" Then 
         Dim lineSplit As String() = 
         line.Split(New [Char]() {","})                       
      If lineSplit.GetValue(5) <> tagids 
       Then tagids(tagidcount) = (lineSplit.GetValue(5))         
       Tagidcount += 1                       
       End If
    End If

    But once again I'm getting a Null Reference Exception...

    Any ideas on this one?

    Thanks for the help so far.

    EDIT: I figured it out. Going to use a List instead of an array on this one.

    • Edited by Mellonjollie Wednesday, December 12, 2012 9:28 PM Update
    Wednesday, December 12, 2012 8:54 PM
  • See if the code below works.  I made a new list() of unique values and used the contain() method to see if the value alread exists.

    Sub Main()
            Dim filereader As StreamReader
            Dim inputline As String
            Dim linestartnum As Integer = CInt(linestart)
            Dim linecounter As Integer
            Dim sb As StringBuilder
            Dim uniquevalues As List(Of String) = New List(Of String)
            Dim splitline() As String
            Do While filereader.EndOfStream = False
                inputline = filereader.ReadLine()
                If Trim(inputline).Length > 0 Then
                    Select Case linecounter
                        Case Is <= linestartnum
                            sb.AppendLine(inputline)
                        Case Else
                            splitline = inputline.Split(",")
                            If Not uniquevalues.Contains(splitline(5)) Then
                                sb.AppendLine(splitline(5))
                                uniquevalues.Add(splitline(5))
                            End If
                    End Select
                End If
            Loop
        End Sub


    jdweng

    Wednesday, December 12, 2012 10:25 PM