Welcome to Dream.In.Code
Become a VB Expert!

Join 150,003 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,357 people online right now. Registration is fast and FREE... Join Now!




For-Next Search code error

 
Reply to this topicStart new topic

For-Next Search code error

Panther85
19 Nov, 2006 - 01:38 PM
Post #1

New D.I.C Head
*

Joined: 19 Nov, 2006
Posts: 6


My Contributions
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.

CODE

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 Nov, 2006 - 01:38 PM
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: For-Next Search Code Error
19 Nov, 2006 - 05:09 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Panther85
RE: For-Next Search Code Error
19 Nov, 2006 - 06:04 PM
Post #3

New D.I.C Head
*

Joined: 19 Nov, 2006
Posts: 6


My Contributions
I'm still not quite sure how to fix this problem. All my arrays are declared in the at the form level.
CODE

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.

CODE

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.
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: For-Next Search Code Error
19 Nov, 2006 - 06:06 PM
Post #4

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
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
User is offlineProfile CardPM
+Quote Post

Panther85
RE: For-Next Search Code Error
19 Nov, 2006 - 06:45 PM
Post #5

New D.I.C Head
*

Joined: 19 Nov, 2006
Posts: 6


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 08:37PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month