4 Replies - 1862 Views - Last Post: 19 November 2006 - 07:45 PM Rate Topic: -----

#1 Panther85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-November 06

For-Next Search code error

Post icon  Posted 19 November 2006 - 02:38 PM

Ok I am having a slight problem. For my class I am supposed to create a program that allows a user to enter part of an author's name, part of the title of the book, or part of the ISBN number and shows the title of matches or partial matches in a listbox. It also allows the user to click the title in the listbox and it displays the author's name, title of the book, ISBN number, price, and section in respective textboxes. My problem I am encountering is in my search structure.

Private Sub btnTitle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTitle.Click
		Dim strSearchString As String
		Dim blnFound As Boolean
		Dim strATitle As String

		'Initialize boolean variable to false
		blnFound = False

		'Validate input in title textbox
		If txtTitle.Text = "" Then
			txtTitle.Text = InputBox("Please enter a title.", "Title required")
			Exit Sub
		End If

		'Get input value
		strSearchString = txtTitle.Text

		'Search array and display all matches
		For Each strATitle In strTitle
			If InStr(UCase(strATitle), UCase(strSearchString)) > 0 Then
				lstBooks.Items.Add(strATitle)
				blnFound = True
			End If
		Next

		'Handle No Match situation
		If Not blnFound Then
			MsgBox("No match found.  Please try again.", MsgBoxStyle.Exclamation, "No match found")
			txtTitle.Text = ""
			txtTitle.Focus()
		End If

	End Sub



My arrays all load properly and work fine but when I hit the search button I get a System.NullReferenceException error and the additional information it gives me is that the "Object reference not set to instance of an object." I have code identical to this in another search program, using different variable names, and it works just fine. Anyone have any clues?

This post has been edited by Panther85: 19 November 2006 - 02:38 PM


Is This A Good Question/Topic? 0
  • +

Replies To: For-Next Search code error

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: For-Next Search code error

Posted 19 November 2006 - 06:09 PM

it doesn't look like strTitle is being initialized to anything. It is created as a String but is still null or '' in this case, i believe that is your error, the array has not be set to this variable.

also it is bad programming style in VB to leave the command next open eg:

Next

It should be followed by the variable which is to be changed in the 'next' rotation through. eg:

Next strATitile


other than that your style seems fine, good variable names, etc.
Was This Post Helpful? 0
  • +
  • -

#3 Panther85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-November 06

Re: For-Next Search code error

Posted 19 November 2006 - 07:04 PM

I'm still not quite sure how to fix this problem. All my arrays are declared in the at the form level.
Dim strISBN() As String
	Dim strAuthor() As String
	Dim strTitle() As String
	Dim decPrice() As Decimal
	Dim strSection() As String
	Dim intNumRecords As Integer




Maybe it is a problem with my coding for the arrays.

Private Sub frmBookLocator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'Declare variables
		Dim strLine As String
		Dim strFileName As String

		'Declare array to hold elements of a line of data
		Dim strFields() As String

		'Declare variable that is the delimiter
		Dim chrDelimiter As Char = ","

		'Define file name
		strFileName = CurDir() & "\booklist.txt"

		'Declare instance of stream reader and assign it to process the input file
		Dim srdReadFile As System.IO.StreamReader = New System.IO.StreamReader(strFileName)

		'Loop to input and process file
		Do Until srdReadFile.Peek - 1
			'Resize program arrays
			ReDim Preserve strISBN(intNumRecords)
			ReDim Preserve strAuthor(intNumRecords)
			ReDim Preserve strTitle(intNumRecords)
			ReDim Preserve decPrice(intNumRecords)
			ReDim Preserve strSection(intNumRecords)

			'Read a line from file
			strLine = srdReadFile.ReadLine()

			strFields = strLine.Split(chrDelimiter)

			'Put each piece into correct program array
			strISBN(intNumRecords) = strFields(0)
			strAuthor(intNumRecords) = strFields(1)
			strTitle(intNumRecords) = strFields(2)
			decPrice(intNumRecords) = strFields(3)
			strSection(intNumRecords) = strFields(4)

			'Increment record counter
			intNumRecords = intNumRecords + 1

		Loop

		'Close the file
		FileClose()

		'Display message declaring arrays loaded
		MsgBox("Arrays have been loaded.", MsgBoxStyle.OKOnly, "Array load complete")

	End Sub



If any of this looks wrong please let me know. I don't get an error message when it runs and I have double checked and made sure the file is in the right spot.

Thanks for the help, everyone.
Was This Post Helpful? 0
  • +
  • -

#4 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Re: For-Next Search code error

Posted 19 November 2006 - 07:06 PM

try simply displaying the arrays onto a txtfield or some other item, to see that they are populated properly, it looks correct but it's the only way to know for sure
Was This Post Helpful? 0
  • +
  • -

#5 Panther85  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-November 06

Re: For-Next Search code error

Posted 19 November 2006 - 07:45 PM

Thanks for the help. I figured it out.

I had Do Until srdReadFile.Peek -1 rather than Do Until srdReadFile.Peek = -1.

Little mistakes will kill you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1