Results 1 to 4 of 4

Thread: File Reading Error?

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    2

    Question File Reading Error?

    Hello!

    I have the following code under my button:
    Code:
        Private Sub SearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchButton.Click
            MsgBox(SearchBox.Text())
            If (File.Exists("Database.bin")) And (SearchBox.Text() <> Nothing) Then
                Dim ioFile As New StreamReader("Database.bin")
                Dim ioLine As String ' Holds 1 Line at a Time
                Dim ioLines As String ' Holds Whole File
                ioLines = ""
                Do
                    ioLine = ioFile.ReadLine()
                    If (ioLine.IndexOf(SearchBox.Text()) <> -1) Then
                        If (ioLine <> Nothing) Then
                            DisplayBox.Items.Add(ioLine)
                        End If
                    End If
                    ioLines = ioLines & vbCrLf & ioLine
                Loop Until ioLine Is Nothing
                'MsgBox(ioLines)
            Else
                MsgBox("Database.bin is missing! Please re-download the program.")
            End If
        End Sub
    It is supposed to open "Database.bin", search the file for whatever the user types into SearchBox (a text box), and add to DisplayBox (a list box) every line of that file that contains the string the user searched. However, it always returns this exception:
    Code:
    System.NullReferenceException was unhandled
      Message=Object reference not set to an instance of an object.
      Source=Universal Handbook
      StackTrace:
           at Universal_Handbook.MainWindow.SearchButton_Click(Object sender, EventArgs e) in C:\Users\Cecil\documents\visual studio 2010\Projects\Universal Handbook\Universal Handbook\Form1.vb:line 21
           at System.Windows.Forms.Control.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ButtonBase.WndProc(Message& m)
           at System.Windows.Forms.Button.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(ApplicationContext context)
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
           at Universal_Handbook.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException:
    Could someone please inform me of what is wrong and help me correct it?

    Here is Database.bin:
    Code:
    1010000    Long Brown Beard    
    1010001    Goatee    
    1010002    Ninja Mask for Men    
    1010003    5 O'Clock Shadow    
    1010004    General's Mustache (1)    
    1010005    General's Mustache (2)    
    1010006    Yakuza Scar    
    1011000    Ninja Mask for Women    
    1011001    SF Ninja Mask    
    1011002    Heart    
    1011003    Freckles    
    1012000    Battle Scar    
    1012001    Bindi    
    1012002    Leather Mask    
    1012003    Blush    
    1012004    Disguise    
    1012005    Bruise    
    1012006    Rose    
    1012007    Santa Beard    
    1012008    Censor    
    1012009    Kiss Mark    
    1012010    Hinomaru
    Help is greatly appreciated!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: File Reading Error?

    You have your Until clause at the end of the loop, which means that a line will already have been read and processed before you make that check, by which time it's too late. If you want to read a file line by line using a StreamReader then do so like this:
    vb.net Code:
    1. Using reader As New IO.StreamReader("file path here")
    2.     Do Until reader.EndOfStream
    3.         Dim line = reader.ReadLine()
    4.  
    5.         'Use line here.
    6.     Loop
    7. End Using
    If you want the items in a ListBox and the entire text elsewhere though, there's an easier way:
    vb.net Code:
    1. Dim lines As String() = IO.File.ReadAllLines("file path here")
    2.  
    3. Me.ListBox1.DataSource = lines
    4.  
    5. Dim content As String = String.Join(Environment.NewLine, lines)
    Finally, it doesn't really make sense to use a ".bin" extension, which implies binary data, to store text. A ".dat" extension, implying data, would be more appropriate.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    2

    Re: File Reading Error?

    Oh okay. I see now, thanks. I am pretty new to VB and I'm used to C++. And by the way, I was just using ".bin" to try and "hide" the file from any users of the program, though I guess a ".dat" would be more appropriate.

  4. #4
    Hyperactive Member marniel647's Avatar
    Join Date
    Aug 2010
    Location
    MSDN Library
    Posts
    259

    Re: File Reading Error?

    Quote Originally Posted by jmcilhinney View Post
    You have your Until clause at the end of the loop, which means that a line will already have been read and processed before you make that check, by which time it's too late. If you want to read a file line by line using a StreamReader then do so like this:
    vb.net Code:
    1. Using reader As New IO.StreamReader("file path here")
    2.     Do Until reader.EndOfStream
    3.         Dim line = reader.ReadLine()
    4.  
    5.         'Use line here.
    6.     Loop
    7. End Using
    If you want the items in a ListBox and the entire text elsewhere though, there's an easier way:
    vb.net Code:
    1. Dim lines As String() = IO.File.ReadAllLines("file path here")
    2.  
    3. Me.ListBox1.DataSource = lines
    4.  
    5. Dim content As String = String.Join(Environment.NewLine, lines)
    Finally, it doesn't really make sense to use a ".bin" extension, which implies binary data, to store text. A ".dat" extension, implying data, would be more appropriate.
    thanks for this JM..... i learn now how to read a file..

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