none
VB.net Windows Form, Object reference not set to an instance of an object

    Question

  • I am getting this error "Object reference not set to an instance of an object" and I dont know where/how to fix it and it does not tell me where the error in the code occurs, just a pop-up windows saying the error. Here is the code on the "start up" Windows form...Thanks

     

    Option Explicit On
    
    Imports System
    
    Imports System.Data
    
    Imports System.Windows.Forms.Form
    
    Imports OaSIS.ovary
    
    Imports OaSIS.oasis_patient
    
    Imports OaSIS.health_assessment
    
    Imports OaSIS.systemic_therapy
    
    Imports OaSIS.surgery
    
    Imports OaSIS.staging
    
    Imports OaSIS.radiation_therapy
    
    Imports OaSIS.psychological_screening
    
    Imports OaSIS.nutrition_screening
    
    Imports OaSIS.cais
    
    
    
    Public Class main_menu
    
    
    
     Private group_name As String
    
     Private gbl_oasis_id As String
    
     Private gbl_agency_id As String
    
     Private gbl_surname As String
    
     Private gbl_first_name As String
    
     Private gbl_event_date As String
    
    
    
     Private Sub main_menu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    
    
      For Each f As Form In Application.OpenForms
    
       If f.Modal Then
    
        If f.Name <> "main_menu" Then
    
         f.Dispose()
    
        End If
    
       End If
    
      Next
    
    
    
      ' Set the title of the form.
    
      Dim ApplicationTitle As String
    
      If My.Application.Info.Title <> "" Then
    
       ApplicationTitle = My.Application.Info.Title
    
      Else
    
       ApplicationTitle = System.IO.Path.GetFileNameWithoutExtension(My.Application.Info.AssemblyName)
    
      End If
    
      Me.Text = String.Format("{0}", ApplicationTitle)
    
      ' Initialize all of the text displayed on the About Box.
    
      ' TODO: Customize the application's assembly information in the "Application" pane of the project 
    
      ' properties dialog (under the "Project" menu).
    
    
    
      Me.lbl_product_name.Text = My.Application.Info.ProductName
    
    
    
      Me.lbl_version.Text = String.Format("Version {0}", My.Application.Info.Version.ToString)
    
    
    
      Me.lbl_copyright.Text = My.Application.Info.Copyright
    
    
    
      Me.lbl_server_name.Text = Mid(My.Settings.oasis_connection, 13, InStr(My.Settings.oasis_connection, ";") - 13)
    
    
    
      Dim s As String = My.User.Name
    
      If s.Contains("\") Then
    
       Me.lbl_user_name.Text = "User Name: " & s.Substring(s.IndexOf("\") + 1)
    
      Else
    
       Me.lbl_user_name.Text = "User Name: " & s
    
      End If
    
    
    
    
    
      Me.txt_description.Text = ""
    
    
    
      Me.KeyPreview = True
    
    
    
      'application_start_up()
    
    
    
      Me.lbl_user_group.Text = "User Group: " & group_name
    
    
    
      'Select Case group_name
    
      ' Case ""
    
      '  Me.Close()
    
    
    
      ' Case "Developer"
    
      'End Select
    
    
    
      Me.lb_tumour_group.Focus()
    
    
    
     End Sub
    
    
    
     Public Sub application_start_up()
    
      Dim DBConnection As New SqlClient.SqlConnection(OaSIS.My.MySettings.Default.oasis_connection)
    
      Dim application_start_up As New SqlClient.SqlCommand("ap_tga_application_start_up", DBConnection)
    
      application_start_up.CommandType = CommandType.StoredProcedure
    
      Dim application_start_up_parameter As SqlClient.SqlParameter = application_start_up.CreateParameter
    
      DBConnection.Open()
    
    
    
      '@arg_user_name
    
      Dim user_name As String = My.User.Name.ToString
    
      application_start_up.Parameters.AddWithValue("@arg_user_name", user_name)
    
    
    
      '@arg_group_name
    
      application_start_up.Parameters.Add("@arg_group_name", SqlDbType.VarChar, 128)
    
      application_start_up.Parameters("@arg_group_name").Direction = ParameterDirection.Output
    
    
    
      Try
    
       application_start_up.ExecuteNonQuery()
    
      Catch ex As System.Exception
    
       System.Windows.Forms.MessageBox.Show(ex.Message)
    
       Exit Sub
    
      End Try
    
    
    
      group_name = application_start_up.Parameters("@arg_group_name").Value.ToString
    
    
    
      '-----Clean up
    
      application_start_up = Nothing
    
      DBConnection.Close()
    
      DBConnection.Dispose()
    
    
    
     End Sub
    
    
    
     Public Sub application_shut_down()
    
      Dim DBConnection As New SqlClient.SqlConnection(OaSIS.My.MySettings.Default.oasis_connection)
    
      Dim application_shut_down As New SqlClient.SqlCommand("ap_tga_application_shut_down", DBConnection)
    
      application_shut_down.CommandType = CommandType.StoredProcedure
    
      Dim application_shut_down_parameter As SqlClient.SqlParameter = application_shut_down.CreateParameter
    
      DBConnection.Open()
    
    
    
      '@arg_user_name
    
      Dim user_name As String = My.User.Name.ToString
    
      application_shut_down.Parameters.AddWithValue("@arg_user_name", user_name)
    
    
    
      Try
    
       application_shut_down.ExecuteNonQuery()
    
      Catch ex As System.Exception
    
       System.Windows.Forms.MessageBox.Show(ex.Message)
    
       Exit Sub
    
      End Try
    
    
    
      '-----Clean up
    
      application_shut_down = Nothing
    
      DBConnection.Close()
    
      DBConnection.Dispose()
    
    
    
     End Sub
    
    
    
     Public Property get_oasis_id() As String
    
    
    
      Get
    
       Return gbl_oasis_id
    
      End Get
    
    
    
      Set(ByVal Value As String)
    
       gbl_oasis_id = Value
    
      End Set
    
    
    
     End Property
    
    
    
     Public Property get_agency_id() As String
    
    
    
      Get
    
       Return gbl_agency_id
    
      End Get
    
    
    
      Set(ByVal Value As String)
    
       gbl_agency_id = Value
    
      End Set
    
    
    
     End Property
    
    
    
     Public Property get_surname() As String
    
    
    
      Get
    
       Return gbl_surname
    
      End Get
    
    
    
      Set(ByVal Value As String)
    
       gbl_surname = Value
    
      End Set
    
    
    
     End Property
    
    
    
     Public Property get_first_name() As String
    
    
    
      Get
    
       Return gbl_first_name
    
      End Get
    
    
    
      Set(ByVal Value As String)
    
       gbl_first_name = Value
    
      End Set
    
    
    
     End Property
    
    
    
     Public Property get_user_group() As String
    
    
    
      Get
    
       Return group_name
    
      End Get
    
    
    
      Set(ByVal Value As String)
    
       group_name = Value
    
      End Set
    
    
    
     End Property
    
    
    
     Public Property get_event_date() As String
    
    
    
      Get
    
       Return gbl_event_date
    
      End Get
    
    
    
      Set(ByVal Value As String)
    
       gbl_event_date = Value
    
      End Set
    
    
    
     End Property
    
    
    
     Public Sub ApplicationExit(ByVal sender As Object, ByVal e As EventArgs)
    
      'application_shut_down()
    
     End Sub
    
    
    
     Private Sub btn_exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_exit.Click
    
      'application_shut_down()
    
      Me.Close()
    
     End Sub
    
    
    
     Private Sub btn_proceed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_proceed.Click
    
      If Me.lb_tumour_group.SelectedItems.Count <> 0 Then
    
       Me.Visible = False
    
       Dim t As String = Me.lb_tumour_group.SelectedValue.ToString
    
       Dim main_form As New Form
    
       Select Case t
    
        Case "LU"
    
         main_form = My.Forms.lung
    
         My.Forms.lung.lu_lbl_oasis_title.Text = "Lung"
    
         My.Forms.lung.lu_oasis_patient.Tag = t
    
         My.Forms.lung.lu_oasis_patient.op_dgv_oasis_patient_event_list.Tag = t
    
    
    
         main_form.Show()
    
         My.Forms.lung.lu_tc_lung.SelectTab("lu_tp_oasis_patient")
    
    
    
        Case "HN"
    
         main_form = My.Forms.head_and_neck
    
         My.Forms.head_and_neck.hn_lbl_oasis_title.Text = "Head and Neck"
    
         My.Forms.head_and_neck.hn_oasis_patient.Tag = t
    
         My.Forms.head_and_neck.hn_oasis_patient.op_dgv_oasis_patient_event_list.Tag = t
    
    
    
         main_form.Show()
    
         My.Forms.head_and_neck.hn_tc_head_neck.SelectTab("hn_tp_oasis_patient")
    
    
    
        Case "GY-OV"
    
         main_form = My.Forms.ovary
    
         My.Forms.ovary.ov_lbl_oasis_title.Text = "Gynecology - Ovary"
    
         My.Forms.ovary.ov_oasis_patient.Tag = t
    
         My.Forms.ovary.ov_oasis_patient.op_dgv_oasis_patient_event_list.Tag = t
    
    
    
         main_form.Show()
    
         My.Forms.ovary.ov_tc_ovary.SelectTab("ov_tp_oasis_patient")
    
    
    
        Case Else
    
         MessageBox.Show("Selected tumour group application is under developed.")
    
         Me.Visible = True
    
    
    
       End Select
    
    
    
      Else
    
       MessageBox.Show("Please select a tumour group on the left and click on the button to proceed")
    
      End If
    
    
    
     End Sub
    
    
    
     Private Sub lb_tumour_group_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles lb_tumour_group.GotFocus
    
    
    
      'tumour group list
    
      Try
    
       Me.ta_oasis_lookup_001_tumour_group.Fill(Me.ds_oasis_lookup_001_tumour_group.ap_oasis_lookup_001_TumourGroup, My.User.Name.ToString)
    
      Catch ex As System.Exception
    
       System.Windows.Forms.MessageBox.Show(ex.Message)
    
      End Try
    
     End Sub
    
    
    
    End Class
    
    

     

    • Moved by Helen Zhou Wednesday, April 28, 2010 1:23 AM (From:Windows Forms General)
    Tuesday, April 27, 2010 9:56 PM

Answers

  • It means that somewhere are you using an address which should reference to an object.

    But that reference does not contain an object.

    You are doing your forms let say a little bit in an ordinary way.

    I assume that it is in code like this one.

    main_form = My.Forms.ovary

         My.Forms.ovary.ov_lbl_oasis_title.Text = "Gynecology - Ovary"

         My.Forms.ovary.ov_oasis_patient.Tag = t

         My.Forms.ovary.ov_oasis_patient.op_dgv_oasis_patient_event_list.Tag = t

         main_form.Show()


    Success
    Cor
    Wednesday, April 28, 2010 7:33 AM
  •  

    Hi pmak ,

    Go to the VIEW menu and select SOLUTION EXPLORER ( shortcut = Ctrl+R )

    At the top select SHOW ALL FILES as in this picture.>>

     

     

    Click the little + beside the Form name.

    Post the code from your Form1.Designer.Vb file here if you wish and I will have a look over it, or from the name of your Form.

     

    As Cor Ligthert MVP has said, the error means that something does not have an object assigned to it or that an array or other collection element may be empty.

    You may need to use a NEW somewhere in your code.

    E.G:

    Dim sr As New System.Io.StreamReader

    as in.>>

     

    Dim sr As New System.IO.StreamReader(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\SomeFile.txt")

     

    rather than without the NEW

    Dim sr As System.Io.StringBuilder

     

    Regards,

    John

    P.S. Your question is one of the most common questions in this forum, please try to use the forum search box 1st

    just above the list of forum questions and discussions ( threads ).

    See these forum search results, there are now over 600 of them.>>

    http://social.msdn.microsoft.com/Search/en-US/?query=Object%20reference%20not%20set%20to%20an%20instance%20of%20an%20object &rq=meta:Search.MSForums.GroupID%28729d4ce2-2138-4ab2-a688-c1e5c0470cce%29+site:microsoft.com&rn=All+Visual+Basic+Forums

     

    Please see this thread for Vb.Net learning links.>> http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/549c8895-6780-42f8-878f-2138214fdeb4
    Wednesday, April 28, 2010 1:53 PM
  •  I assigned a breakpoint to teh form_load event.


    Just to add to what the others have said... Please be aware that the Load event is not the first thing to happen when you start up your application.  To begin with, any declared variables will be visited first if there is an in-line assignment to that variable -- these will be visited in the order they appear within the code file.  Next is the constructor of the class.  In the case of a form this will require a very first call to InitializeComponents (in the designer file) which is what John was pointing out to you.  Next will be any subsequent code in the constructor (which you apparently do not have any).  You'd think that the Load event would be next but this is not always the case.  If you are handling other events that occur during activation or focus events then these might be hit numerous times before it gets to Load.

    A common issue is that one of the variables is a reference type that has its own constructor code where an exception may occur (in other words, the non-reference exception may not be in your form's code).  Also, as John was suggesting as a likely cuprit, the initializing code for the form's controls could experience errors.  This is especially true when you use custom controls -- it can be a real headache if you're not a careful coder.  Coincidently, this is typically why occassionally the form will fail to render when you try to open it up, resulting in an error page.

    In addition to setting breakpoints in the designer you can also just press F11 to step into the program at the first hit line (typically a variable setting if not in the form's constructor).  If you still never get to the first line of code this way then you probably have a faulty or corrupt library that is being referenced.

    Good luck!

    Wednesday, April 28, 2010 7:53 PM

All replies

  • Try stepping through your code one line at a time so you'll know where the exception occurs.
    Tuesday, April 27, 2010 10:11 PM
  • The error does not appear in the coding of the Windows form. I assigned a breakpoint to teh form_load event. The error message pop up before the form_load event start and it appears three times and I clcik "Ok" to the message and it disappear and then the Windows form load without a problem.  Thanks.
    Tuesday, April 27, 2010 11:36 PM
  • Hi pmark,

    I'm moving this thread from ”Windows Forms General" to "Visual Basic General" forum, since this issue is related to VB. There are more VB experts in the "Visual Basic General" forum.

    Sincerely,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Wednesday, April 28, 2010 1:26 AM
  • It means that somewhere are you using an address which should reference to an object.

    But that reference does not contain an object.

    You are doing your forms let say a little bit in an ordinary way.

    I assume that it is in code like this one.

    main_form = My.Forms.ovary

         My.Forms.ovary.ov_lbl_oasis_title.Text = "Gynecology - Ovary"

         My.Forms.ovary.ov_oasis_patient.Tag = t

         My.Forms.ovary.ov_oasis_patient.op_dgv_oasis_patient_event_list.Tag = t

         main_form.Show()


    Success
    Cor
    Wednesday, April 28, 2010 7:33 AM
  •  

    Hi pmak ,

    Go to the VIEW menu and select SOLUTION EXPLORER ( shortcut = Ctrl+R )

    At the top select SHOW ALL FILES as in this picture.>>

     

     

    Click the little + beside the Form name.

    Post the code from your Form1.Designer.Vb file here if you wish and I will have a look over it, or from the name of your Form.

     

    As Cor Ligthert MVP has said, the error means that something does not have an object assigned to it or that an array or other collection element may be empty.

    You may need to use a NEW somewhere in your code.

    E.G:

    Dim sr As New System.Io.StreamReader

    as in.>>

     

    Dim sr As New System.IO.StreamReader(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\SomeFile.txt")

     

    rather than without the NEW

    Dim sr As System.Io.StringBuilder

     

    Regards,

    John

    P.S. Your question is one of the most common questions in this forum, please try to use the forum search box 1st

    just above the list of forum questions and discussions ( threads ).

    See these forum search results, there are now over 600 of them.>>

    http://social.msdn.microsoft.com/Search/en-US/?query=Object%20reference%20not%20set%20to%20an%20instance%20of%20an%20object &rq=meta:Search.MSForums.GroupID%28729d4ce2-2138-4ab2-a688-c1e5c0470cce%29+site:microsoft.com&rn=All+Visual+Basic+Forums

     

    Please see this thread for Vb.Net learning links.>> http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/549c8895-6780-42f8-878f-2138214fdeb4
    Wednesday, April 28, 2010 1:53 PM
  •  I assigned a breakpoint to teh form_load event.


    Just to add to what the others have said... Please be aware that the Load event is not the first thing to happen when you start up your application.  To begin with, any declared variables will be visited first if there is an in-line assignment to that variable -- these will be visited in the order they appear within the code file.  Next is the constructor of the class.  In the case of a form this will require a very first call to InitializeComponents (in the designer file) which is what John was pointing out to you.  Next will be any subsequent code in the constructor (which you apparently do not have any).  You'd think that the Load event would be next but this is not always the case.  If you are handling other events that occur during activation or focus events then these might be hit numerous times before it gets to Load.

    A common issue is that one of the variables is a reference type that has its own constructor code where an exception may occur (in other words, the non-reference exception may not be in your form's code).  Also, as John was suggesting as a likely cuprit, the initializing code for the form's controls could experience errors.  This is especially true when you use custom controls -- it can be a real headache if you're not a careful coder.  Coincidently, this is typically why occassionally the form will fail to render when you try to open it up, resulting in an error page.

    In addition to setting breakpoints in the designer you can also just press F11 to step into the program at the first hit line (typically a variable setting if not in the form's constructor).  If you still never get to the first line of code this way then you probably have a faulty or corrupt library that is being referenced.

    Good luck!

    Wednesday, April 28, 2010 7:53 PM