Object Reference not set to an instance of an object

JohnOb

Newcomer
Joined
Mar 22, 2006
Messages
11
Hi All,
I am in .NET 2003

I have a form and a toolbar on the form.
When the user clicks on a button on the toolbar I call
the following Sub

I get a crash on the last line: tlbSearch.Buttons(0).Enabled = True ' enable search

An unhandled exception of type "System.NullreferenceExecption" occured etc.....
Object Reference not set to an instance of an object

How can this be?, near the stop of the Sub I execute: tlbSearch.Buttons(0).Enabled = False ' Disable search
with no problem.
What could have happended to the toolbar object?
I am new to objects, I come from a VB background.

Thanks in Advance,
Visual Basic:
'-------------------------------------------------------------------

Public Sub DoTheSearch()

   Dim intDX As Integer
   Dim Structures As New clsStructures
   Dim clsMiscProjMulti As New     MiscProjectMultiTable_Handler(Permissions.ConnectionString)
   Dim SearchDataReader As SqlClient.SqlDataReader
   Dim blnWhere As Boolean = False
   Dim TheRetrievalType As RetrievalType
   Dim NotFoundMsg As String

   Dim strSelectString = "SELECT  ProjectID,Reg,Fee,Title," & _
                                  "MiscFeeTypeID," & _
                                  "Fee, Quarter, [Year],MiscProjectStatusID," & _
                                  "StatusDate,Miscellaneous_Project.Account_ID,FinalizedDate," & _
                                  "ContactEmployeeID,ContactEmail," & _
                                  "Unex_Employee.EmpFirstName + ' ' +  Unex_Employee.EmpLastName As EmpName," & _
                                  "Account.Exp_Account + '-' +  Account.Cost_Center As ExpAcctCC " & _
                          "FROM   Miscellaneous_Project " & _
                                  "Left Outer Join Unex_Employee " & _
                                  "On  Miscellaneous_Project.ContactEmployeeID = Unex_Employee.EmployeeID " & _
                                  "Left Outer Join Account " & _
                                  "On Miscellaneous_Project.Account_ID = Account.Account_ID "
   '----------

   Me.Cursor = System.Windows.Forms.Cursors.WaitCursor()
   
   tlbSearch.Buttons(0).Enabled = False                        ' Disable search - this is ok.
   menuSearch.Enabled = False

   tlbSearch.Buttons(1).Enabled = True                         ' Enable stop 
   menuStop.Enabled = True

   tlbSearch.Buttons(2).Enabled = False                        ' Disable print
   menuPrint.Enabled = False

   tlbSearch.Buttons(4).Enabled = False                        ' Disable the create new button. 
   menuCreateNew.Enabled = False                               ' Disable the create new menu item. 

   tlbSearch.Buttons(5).Enabled = False                        ' Disable Edit
   menuEdit.Enabled = False
   stsSearch.text = "Searching....."

   ' Clear the grid.

   C1FGResults.RowSel = -1
   C1FGResults.ColSel = -1

   If C1FGResults.Rows.Count > 1 Then

      For intDX = (C1FGResults.Rows.Count - 1) To 1 Step -1
          C1FGResults().Rows.Remove(intDX)
      Next

   End If

   '-----------------------------------------------

   If Me.txtRegProjID.Text = vbNullString Then

      ' look for projects using the type, Status and Deparatment
      ' build the query string, using the parameters.

      If c1cboFeeType.SelectedIndex > 0 Then
         strSelectString = strSelectString & " Where Miscellaneous_Project.MiscFeeTypeID = " & Structures.GetComboValue(c1cboFeeType)
         blnWhere = True
      End If

      '----------

      If c1cboProjectStatus.SelectedIndex > 0 Then

         If blnWhere = False Then
            strSelectString = strSelectString & " Where "
            blnWhere = True
         Else
            strSelectString = strSelectString & " And "
         End If

         strSelectString = strSelectString & "Miscellaneous_Project.MiscProjectStatusID = " & Structures.GetComboValue(c1cboProjectStatus)
      End If

      '----------

      If c1cboDept.SelectedIndex > 0 Then

         If blnWhere = False Then
            strSelectString = strSelectString & " Where "
            blnWhere = True
         Else
            strSelectString = strSelectString & " And "
         End If

         strSelectString = strSelectString & "Miscellaneous_Project.Account_ID = " & Structures.GetComboValue(c1cboDept)

      End If

      '----------

      If clsMiscProjMulti.dbSelect_DataReader(strSelectString, SearchDataReader) = True Then

         ' No errors, did we find any records?

         If SearchDataReader.HasRows = True Then

            While SearchDataReader.Read() = True
                  Call AddARow(SearchDataReader)
            End While
         Else
            Me.Cursor = System.Windows.Forms.Cursors.Default()
            MsgBox("Unable to find any projects with the parameters selected.", MsgBoxStyle.Information + MsgBoxStyle.OKOnly, "Not Found.")
         End If
      Else
         Me.Cursor = System.Windows.Forms.Cursors.Default()
         Dim err As New errorHandler(clsMiscProjMulti.lastException, "", True)
      End If

   Else

      ' Did the user enter a project Id or a Reg number? 

      If IsNumeric(Mid(txtRegProjID.Text, 1, 1)) Then
         strSelectString = strSelectString & " Where ProjectID = " & Trim$(txtRegProjID.Text)
         TheRetrievalType = RetrievalType.ProjectID
      Else
         ' the user entered a reg number. 
         strSelectString = strSelectString & " Where Reg = '" & Trim$(txtRegProjID.Text) & "'"
         TheRetrievalType = RetrievalType.RegNumber
      End If

      ' do the read.

      If clsMiscProjMulti.dbSelect_DataReader(strSelectString, SearchDataReader) = True Then

         ' No errors, did we find any records?

         If SearchDataReader.HasRows = True Then
            SearchDataReader.Read()
            Call AddARow(SearchDataReader)
         Else
            Me.Cursor = System.Windows.Forms.Cursors.Default()

            If TheRetrievalType = RetrievalType.ProjectID Then
               NotFoundMsg = "Project ID of : " & Trim$(txtRegProjID.Text)
            Else
               NotFoundMsg = "Reg Number of : " & Trim$(txtRegProjID.Text)
            End If

            MsgBox("Unable to find a project with a " & NotFoundMsg, MsgBoxStyle.Information + MsgBoxStyle.OKOnly, "Not Found.")
          End If

      Else
         Me.Cursor = System.Windows.Forms.Cursors.Default()
         Dim err As New errorHandler(clsMiscProjMulti.lastException, "", True)
      End If

   End If

   '-------------------------------------------------------

   Me.Cursor = System.Windows.Forms.Cursors.Default()

   ' the crash occurs on the next line. 
 
   tlbSearch.Buttons(0).Enabled = True                         ' Enable search

:confused:
 
Last edited by a moderator:
It's quite difficult to understand a large block of code like that, especially without syntax highlighting. In future if you encase VB code in VB code tags it will help people read it. Looking at your code you don't seem to make it clear exactly where the trouble line occurs, if it occurs either after the button has been disposed or before it has been initialised this would cause that error.
Edit: My bad, it's all one continuous block of code isn't it. I was assuming that the '------ seperate the code functionally, whereas its just logically for the reader. Since this is the case I can't see any reason it would have been disposed by this point. Are you certain it's that line of code causing the null reference exception not the following one?
 
Last edited:
The tlbsearch.buttons.count is zero.

How can this be? I am used to VB, not .NET
Do I have to declare and substantiate the toolbar everytime
I refer to a property on it?



:confused:
 
You said it doesn't blow up at the top, when you first reference tlbSearch.Buttons(0) so something must be clearing that collection. I'd step through code to find out when it's getting cleared.

-ner
 
Thank you for advice, I steped through the code and found out I was
doing something in one of the procs to cause the form to be unloaded,
(closed), that in turn was causing the toolbar buttons object collection to not exist.

Thanks Again,

John

Nerseus said:
You said it doesn't blow up at the top, when you first reference tlbSearch.Buttons(0) so something must be clearing that collection. I'd step through code to find out when it's getting cleared.

-ner
:)
 
Back
Top