I'm writing a program to automatically setup some long files for use with streamwriting for another app. Basically all it does is add "sw.WriteLine("" to the beginning of every line and "")" to the end of every line.

You gather a list of files which is entered into a listbox, the problem I'm having is I don't quite know how to have the streamreader read multiple files. I tried to only way I could think of an got the following error:

Object reference not set to an instance of an object.

It works but I don't want to keep getting that error. Here's the code:

Dim FolderBrowserDialog1 As New FolderBrowserDialog
        With FolderBrowserDialog1
            .RootFolder = Environment.SpecialFolder.Desktop
            .SelectedPath = "C:\"
            If .ShowDialog = DialogResult.OK Then
                For Each File As String In ListBox1.Items
                        Using sr As StreamReader = New StreamReader(File)
                            Dim filePath As String = File
                            Dim slashPosition As Integer = filePath.LastIndexOf("\")
                            Dim filenameOnly As String = filePath.Substring(slashPosition + 1)
                            Using sw As StreamWriter = New StreamWriter(.SelectedPath & "\" & filenameOnly)
                                Dim line As String
                                    line = sr.ReadLine()
                                    line = line.Replace("""", """""")
                                    line = line.Insert(0, "sw.WriteLine(""")
                                    line = line.Insert(line.Length, """)")
                                Loop Until line Is Nothing
                            End Using
                            For Each check As String In ListBox1.Items
                                If My.Computer.FileSystem.FileExists(.SelectedPath & "\" & filenameOnly) = True Then
                                    MsgBox(filenameOnly & " was successfuly written", MsgBoxStyle.Information, "Save")
                                    MsgBox("An error occurred saving: " & filenameOnly & ".", MsgBoxStyle.Information, "Save")
                                End If
                        End Using
                    Catch ex As Exception
                    End Try
            End If
        End With

>Object reference not set to an instance of an object.

Use IsNothing() method to check whether an object variable has reference of an object or null.