Question "Object reference not set to an instance of an object" Dialog Box Problem

JamesBowtell

Member
Joined
Feb 26, 2009
Messages
16
Programming Experience
3-5
Hey all,

i'm using Visual Basic 2008 express to create a software booking system with an SQL backend.

i have a search button on a form (frmUserDetails) that when clicked opens up a second form as a dialog box (frmSearch).

when i select an item from a datagrid thats in (frmSearch) the userID is placed in a variable (NuNameTB) the search form closes and the relevent details should be displayed in frmUserDetails.

but i'm getting an error that displays the following message within the frmsearch...(marked in red below)

Object reference not set to an instance of an object

if i add a new instance using Dim fromsoftwareloan As New frm_new_software_loan


the form closes and nothing is updated on the softwareloan form

form1 (frmUserDetails
VB.NET:
  Public Class frm_new_software_loan

    Inherits System.Windows.Forms.Form
    Public frmUserSearch As frm_user_search
    Public userIDsearch As Integer  

Private Sub ToolStripButtonSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButtonSearch.Click

        Dim frmUserSearch As New frm_user_search

        frmUserSearch.ShowDialog(Me)

    End Sub

End Class

form2 frmSearch

VB.NET:
Public Class frm_user_search

    Inherits System.Windows.Forms.Form
    Public Shared NuNameTB As Integer
    Public frmSoftwareLoan As frm_new_software_loan



    Private Sub ButtonSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSearch.Click

        If TextBoxSearch.Text = "" Then

            MessageBox.Show("Please enter search criteria.", "Search", MessageBoxButtons.OK, MessageBoxIcon.Information)

        Else

            Try
                Me.Tbl_userTableAdapter.FillBySname(Me.UserDataSet.tbl_user, TextBoxSearch.Text)
            Catch ex As System.Exception
                System.Windows.Forms.MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub

Public Sub Tbl_userDataGridView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Tbl_userDataGridView.CellClick


        NuNameTB = Me.Tbl_userDataGridView.Rows(e.RowIndex).Cells("DataGridViewTextBoxColumn1").Value


        Try

            'TODO: This line of code loads data into the 'SoftwareLookupDataSet.tbl_software' table. You can move, or remove it, as needed.
            [COLOR="Red"]frmSoftwareLoan.Tbl_softwareTableAdapter.Fill(frmSoftwareLoan.SoftwareLookupDataSet.tbl_software)[/COLOR]

            'TODO: This line of code loads data into the 'ITSoftwareLoansSDataSet.tbl_software_loan' table. You can move, or remove it, as needed.
            frmSoftwareLoan.Tbl_software_loanTableAdapter.Fill(frmSoftwareLoan.ITSoftwareLoansSDataSet.tbl_software_loan)

            'TODO: This line of code loads data into the 'ITSoftwareLoansSDataSet.tbl_user' table. You can move, or remove it, as needed.
            frmSoftwareLoan.Tbl_userTableAdapter.FillByUserID(frmSoftwareLoan.ITSoftwareLoansSDataSet.tbl_user, NuNameTB)

        Catch ex As System.Exception

            System.Windows.Forms.MessageBox.Show(ex.Message)

        End Try

        Me.Close()

    End Sub
End Class
 
In your form2 frmSearch form I don't see where you're actually setting an instance of the 'frmSoftwareLoan' variable.

Try changing this line:
Public frmSoftwareLoan As frm_new_software_loan

To:
Public frmSoftwareLoan As New frm_new_software_loan
 
Diagnosing this kind of error is very easy. When your code halts and the exception helper window is shown, click "Enable Editing" and then start pointing to things on the line where the error occurred.

Highlight (select the text so it goes white/blue like for copy/paste) everything that is a noun (object) and point to the highlighted nouns:

frmSoftwareLoan
frmSoftwareLoan.Tbl_softwareTableAdapter
frmSoftwareLoan.SoftwareLookupDataSet
frmSoftwareLoan.SoftwareLookupDataSet.tbl_software


One of them will be Nothing. Find out why.
 
In your form2 frmSearch form I don't see where you're actually setting an instance of the 'frmSoftwareLoan' variable.

Try changing this line:
Public frmSoftwareLoan As frm_new_software_loan

To:
Public frmSoftwareLoan As New frm_new_software_loan

Hey,

Done that resolved the issue with the error but doesnt display the information in the text boxes.

VB.NET:
        NuNameTB = Me.Tbl_userDataGridView.Rows(e.RowIndex).Cells("DataGridViewTextBoxColumn1").Value

        frmSoftwareLoan.Label1.Text = NuNameTB

        'Try
[COLOR="Red"]
        'TODO: This line of code loads data into the 'SoftwareLookupDataSet.tbl_software' table. You can move, or remove it, as needed.
        frmSoftwareLoan.Tbl_softwareTableAdapter.Fill(frmSoftwareLoan.SoftwareLookupDataSet.tbl_software)

        'TODO: This line of code loads data into the 'ITSoftwareLoansSDataSet.tbl_software_loan' table. You can move, or remove it, as needed.
        frmSoftwareLoan.Tbl_software_loanTableAdapter.Fill(frmSoftwareLoan.ITSoftwareLoansSDataSet.tbl_software_loan)

        'TODO: This line of code loads data into the 'ITSoftwareLoansSDataSet.tbl_user' table. You can move, or remove it, as needed.
        frmSoftwareLoan.Tbl_userTableAdapter.FillByUserID(frmSoftwareLoan.ITSoftwareLoansSDataSet.tbl_user, NuNameTB)
[/COLOR]

        'Catch ex As System.Exception

        '    System.Windows.Forms.MessageBox.Show(ex.Message)

        'End Try

        Me.Close()

its not running this code in the search form.. but if i run the same code on form 2 where the information displays and just fill the tableadapter with a variable from the search form and run it from a button it works...

not sure where i'm going wrong

any other advice?
 
Back
Top