|
-
Apr 14th, 2011, 06:08 PM
#1
Thread Starter
New Member
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!
-
Apr 15th, 2011, 12:16 AM
#2
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:
Using reader As New IO.StreamReader("file path here") Do Until reader.EndOfStream Dim line = reader.ReadLine() 'Use line here. Loop End Using
If you want the items in a ListBox and the entire text elsewhere though, there's an easier way:
vb.net Code:
Dim lines As String() = IO.File.ReadAllLines("file path here") Me.ListBox1.DataSource = lines 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.
-
Apr 15th, 2011, 07:44 AM
#3
Thread Starter
New Member
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.
-
Apr 15th, 2011, 08:55 AM
#4
Hyperactive Member
Re: File Reading Error?
 Originally Posted by jmcilhinney
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:
Using reader As New IO.StreamReader("file path here")
Do Until reader.EndOfStream
Dim line = reader.ReadLine()
'Use line here.
Loop
End Using
If you want the items in a ListBox and the entire text elsewhere though, there's an easier way:
vb.net Code:
Dim lines As String() = IO.File.ReadAllLines("file path here")
Me.ListBox1.DataSource = lines
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|