Question: What in the world is causing this error message? Object referenced not set to an instance of an object. It happens when searching by Customer
What in the world is causing this error message? Object referenced not set to an instance of an object. It happens when searching by Customer number.
Imports System.IO
Public Class MainForm
Dim txtFile As StreamReader ' Object variable
Dim searchFile As StreamReader ' Object variable
Dim txtfileName As String
Dim intCounter As Integer
Structure CustomerAccounts ' Structure declaration
Dim LastName As String
Dim FirstName As String
Dim CustomerNumber As String
Dim Address As String
Dim City As String
Dim State As String
Dim ZIPCode As Integer
Dim TelephoneNumber As String
Dim AccountBalance As Double
Dim DateOfLastPayment As String
End Structure
Dim CSearchRecord As CustomerAccounts = New CustomerAccounts ' Define search record
Dim CustomerRecord As CustomerAccounts ' Define structure variable
Dim fileName As String
Dim LastName As String
Dim FirstName As String
Dim CustomerNumber As String
Dim Address As String
Dim City As String
Dim State As String
Dim ZIPCode As Integer
Dim TelephoneNumber As String
Dim AccountBalance As Double
Dim DateOfLastPayment As String
Private Sub mnuEditAddNewRecord_Click(sender As Object, e As EventArgs) Handles mnuEditAddNewRecord.Click
' Create an instance of the AddForm
Dim frmAddForm As New AddForm
' Display the form in modal style.
frmAddForm.ShowDialog()
End Sub
Private Sub mnuFileOpen_Click(sender As Object, e As EventArgs) Handles mnuFileOpen.Click
' ID the file to open.
txtfileName = "Records.txt"
' Open the file to read the records.
txtFile = File.OpenText(txtfileName)
' Use the setData function to place the file data into the text boxes
setData()
End Sub
Public Sub setData()
' Increment the counter by 1.
intCounter += 1
' check to see if the file is empty.
If Not txtFile.EndOfStream Then
' Palcce data into the text boxes.
txtLName.Text = txtFile.ReadLine()
txtFName.Text = txtFile.ReadLine()
txtCustNum.Text = txtFile.ReadLine()
txtAddress.Text = txtFile.ReadLine()
txtCity.Text = txtFile.ReadLine()
txtState.Text = txtFile.ReadLine()
txtZip.Text = txtFile.ReadLine()
txtTelNum.Text = txtFile.ReadLine()
txtAcctBal.Text = txtFile.ReadLine()
txtDatePmt.Text = txtFile.ReadLine()
Else
' Notify the user that the end of file hase been reached.
MessageBox.Show("You are at the end of the file")
End If
End Sub
Private Sub btnNextRec_Click(sender As Object, e As EventArgs) Handles btnNextRec.Click
' run the setData function.
setData()
End Sub
Private Sub clearfields()
' Clear the text fields on the form.
txtLName.Text = ""
txtFName.Text = ""
txtCustNum.Text = ""
txtAddress.Text = ""
txtCity.Text = ""
txtState.Text = ""
txtZip.Text = ""
txtTelNum.Text = ""
txtAcctBal.Text = ""
txtDatePmt.Text = ""
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
clearfields()
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
' Close the form.
Me.Close()
End Sub
Private Sub mnuFileExit_Click(sender As Object, e As EventArgs) Handles mnuFileExit.Click
' Close the form.
Me.Close()
End Sub
Private Sub AboutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AboutToolStripMenuItem.Click
' Tell user about the program.
MessageBox.Show("This application demonstrates various file functions, enjoy.")
End Sub
Private Sub MnuFielPrint_Click(sender As Object, e As EventArgs) Handles MnuFielPrint.Click
printCustomerReport.Print()
End Sub
Private Sub printCustomerReport_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles printCustomerReport.PrintPage
Dim count As Integer = 0
Dim intVertPos As Integer
e.Graphics.DrawString("Customer Account Report", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 200, 10)
' ID the file to open.
txtfileName = "Records.txt"
' Open the file to read the records.
txtFile = File.OpenText(txtfileName)
' Use the setData function to place the file data into the text boxes
setData()
searchFile = File.OpenText(txtfileName)
intVertPos = 50
Try
' Read the file to end of file.
' Print the contents to the PrintDocument.
While Not searchFile.EndOfStream
If count = 10 Then
count = 0
intVertPos += 20
Else
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "Last Name:",
searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular),
Brushes.Black, 15, intVertPos)
intVertPos += 15
count += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "First Name:",
searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular),
Brushes.Black, 15, intVertPos)
intVertPos += 15
count += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "Customer Number:",
searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular),
Brushes.Black, 15, intVertPos)
intVertPos += 15
count += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "Address:",
searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular),
Brushes.Black, 15, intVertPos)
intVertPos += 15
count += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "City:",
searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular),
Brushes.Black, 15, intVertPos)
intVertPos += 15
count += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "State:",
searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular),
Brushes.Black, 15, intVertPos)
intVertPos += 15
count += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "ZIP Code:",
searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular),
Brushes.Black, 15, intVertPos)
intVertPos += 15
count += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "Telephone Number:",
searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular),
Brushes.Black, 15, intVertPos)
intVertPos += 15
count += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "Account Balance:",
searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular),
Brushes.Black, 15, intVertPos)
intVertPos += 15
count += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "Date of Last Payment:",
searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular),
Brushes.Black, 15, intVertPos)
intVertPos += 15
count += 1
End If
End While
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub mnuSearchByCustomerNumber_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuSearchByCustomerNumber.Click
' ID the file to open.
txtfileName = "Records.txt"
' Open the file to read the records.
txtFile = File.OpenText(txtfileName)
Dim cstnumber As String
Dim flag As Integer = 0
' Get the last name from the user.
cstnumber = InputBox("Enter Customer number to search for.")
Try
While Not searchFile.EndOfStream
CSearchRecord.LastName = searchFile.ReadLine()
CSearchRecord.FirstName = searchFile.ReadLine()
CSearchRecord.CustomerNumber = searchFile.ReadLine()
CSearchRecord.Address = searchFile.ReadLine()
CSearchRecord.City = searchFile.ReadLine()
CSearchRecord.State = searchFile.ReadLine()
CSearchRecord.ZIPCode = CInt(searchFile.ReadLine())
CSearchRecord.TelephoneNumber = searchFile.ReadLine()
CSearchRecord.AccountBalance = CDbl(searchFile.ReadLine())
CSearchRecord.DateOfLastPayment = searchFile.ReadLine()
' Compare record with search record.
If CSearchRecord.CustomerNumber.Equals(cstnumber) Then
flag = 1
Exit While
End If
End While
If flag.Equals(1) Then
txtLName.Text = CSearchRecord.LastName.ToString()
txtFName.Text = CSearchRecord.FirstName.ToString()
txtCustNum.Text = CSearchRecord.CustomerNumber.ToString()
txtAddress.Text = CSearchRecord.Address.ToString()
txtCity.Text = CSearchRecord.City.ToString()
txtState.Text = CSearchRecord.State.ToString()
txtZip.Text = CSearchRecord.ZIPCode.ToString()
txtTelNum.Text = CSearchRecord.TelephoneNumber.ToString()
txtAcctBal.Text = CSearchRecord.AccountBalance.ToString()
txtDatePmt.Text = CSearchRecord.DateOfLastPayment.ToString()
Else
' Allert user if record does not exist.
MessageBox.Show("Record does not exist.")
clearFields()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
Everything else works fine. I hope you can help me. I don't have any more hair to pull out!
In this code:
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
