1

I am getting this error while i am trying to read one by one all files in a dir, and also reading the text inside

An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe

Additional information: Object reference not set to an instance of an object.

It is going good only for the first file , but when it starts another loop and gets the secound file from that folder there comes the problem. This is my code

Dim theString As String
        Dim path As String
        Dim StrFile, str_5 As String
        Dim fso As New FileSystemObject
        Dim file As TextStream
        Dim line As String

        'theString = ""
        path = "C:\IN\"

        StrFile = Dir(path & "*.txt")

        Do While StrFile <> ""

            file = fso.OpenTextFile(path & StrFile)
            Do While Not file.AtEndOfLine
                line = file.ReadLine
                If InStr(1, line, line.Substring(142, 1), vbTextCompare) = 5 Then
                    'Debug.Print(StrFile)
                    str_5 = str_5 & line

                End If
            Loop

            file.Close()
            file = Nothing
            fso = Nothing

            StrFile = Dir()
        Loop

1 Answers1

0

Just for other people with the same problem, the answer is to just remove this line fso = Nothing