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
                    Try
                        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
                                Do
                                    line = sr.ReadLine()
                                    line = line.Replace("""", """""")
                                    line = line.Insert(0, "sw.WriteLine(""")
                                    line = line.Insert(line.Length, """)")
                                    sw.WriteLine(line)
                                Loop Until line Is Nothing
                                sr.Close()
                                sw.Close()
                            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")
                                Else
                                    MsgBox("An error occurred saving: " & filenameOnly & ".", MsgBoxStyle.Information, "Save")
                                End If
                            Next
                        End Using
                    Catch ex As Exception
                        MsgBox(ex.Message)
                    End Try
                Next
            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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.