CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2002
    Posts
    113

    Error: Object reference not set to an instance of an object

    Hi there,
    I have a class with the following method in it:

    Code:
     
    Public Class RedControlTracker
        Dim frmainInst As New frmMain
    
        Sub New()
            ' Constructor
            GetRedControl()
        End Sub
    
        Public Function GetRedControl() As Control
            Dim c As Control
            For Each c In frmainInst.Panel3.Controls
                If c.BackColor.Equals(Color.Red) Then
                    Return c
                End If
            Next
        End Function
    End Class
    I have a button in my frmMain form with the following code:
    Code:
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Dim c As New RedControlTracker
            MessageBox.Show("Name of the Red Control is: " & c.GetRedControl.Name)
        End Sub
    When I press the button I get the following error:
    Object reference not set to an instance of an object

    What am I doing wrong?

    There is a control with red backround in my Panel3 control. I have not had this error before, probably because my knowledge of classes is very limited. I can get it to work, doing all the coding in my frmMain (form) class, but it is getting kind of crouded.
    Thank you for any help,
    Hans

  2. #2
    Join Date
    Dec 2002
    Posts
    305

    Re: Error: Object reference not set to an instance of an object

    You need to make the following changes:

    1. Dim frmainInst As frmMain = New frmMain
    2. Dim c As RedControlTracker = New RedControlTracker

    3. You don't need to call getRedControl from Sub New.
    4. Because of 'return c' statement, the sub will only show the first control with red backcolor and get out of the loop. If that is what is intended, fine.

    Good luck.

  3. #3
    Join Date
    Mar 2002
    Posts
    113

    Re: Error: Object reference not set to an instance of an object

    Changed to the following:

    Code:
    Public Class RedControlTracker
        Dim frmainInst As frmMain = New frmMain
    
        Sub New()
            ' Constructor
         End Sub
    
        Public Function GetRedControl() As Control
            Dim c As Control
            For Each c In frmainInst.Panel3.Controls
                If c.BackColor.Equals(Color.Red) Then
                    Return c
                End If
            Next
        End Function
    End Class
    Code:
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Dim c As RedControlTracker = New RedControlTracker
            MessageBox.Show("Name of the Red Control is: " & c.GetRedControl.Name)
        End Sub
    It still throws the same error at the same line of code

    Code:
    MessageBox.Show("Name of the Red Control is: " & c.GetRedControl.Name)
    Hans

  4. #4
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: Error: Object reference not set to an instance of an object

    Here is my solution.


    Code:
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Dim c As New RedControlTracker(Me)
            MessageBox.Show("Name of the Red Control is: " & c.GetRedControl.Name)
        End Sub
    Code:
    Public Class RedControlTracker
        Dim frmainInst As frmMain
    
        Sub New(ByVal frm As frmMain)
            ' Constructor
            frmainInst = frm
        End Sub
    
        Public Function GetRedControl() As Control
            Dim c As Control
            For Each c In frmainInst.Panel3.Controls
                If c.BackColor.Equals(Color.Red) Then
                    Return c
                End If
            Next
        End Function
    End Class

  5. #5
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: Error: Object reference not set to an instance of an object

    Here is more flexible Class you can use to find the first red control.

    Code:
    Public Class Form1
        Inherits System.Windows.Forms.Form
    
    #Region " Windows Form Designer generated code "
    
        Public Sub New()
            MyBase.New()
    
            'This call is required by the Windows Form Designer.
            InitializeComponent()
    
            'Add any initialization after the InitializeComponent() call
    
        End Sub
    
        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
    
        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer
    
        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
        Friend WithEvents Button1 As System.Windows.Forms.Button
        Friend WithEvents Panel1 As System.Windows.Forms.Panel
        Friend WithEvents Panel2 As System.Windows.Forms.Panel
        Friend WithEvents Panel3 As System.Windows.Forms.Panel
        Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.PictureBox1 = New System.Windows.Forms.PictureBox
            Me.Button1 = New System.Windows.Forms.Button
            Me.Panel1 = New System.Windows.Forms.Panel
            Me.Panel2 = New System.Windows.Forms.Panel
            Me.Panel3 = New System.Windows.Forms.Panel
            Me.TextBox1 = New System.Windows.Forms.TextBox
            Me.Label1 = New System.Windows.Forms.Label
            Me.TextBox2 = New System.Windows.Forms.TextBox
            Me.Panel1.SuspendLayout()
            Me.Panel3.SuspendLayout()
            Me.SuspendLayout()
            '
            'PictureBox1
            '
            Me.PictureBox1.Location = New System.Drawing.Point(46, 327)
            Me.PictureBox1.Name = "PictureBox1"
            Me.PictureBox1.Size = New System.Drawing.Size(760, 149)
            Me.PictureBox1.TabIndex = 0
            Me.PictureBox1.TabStop = False
            '
            'Button1
            '
            Me.Button1.Location = New System.Drawing.Point(73, 16)
            Me.Button1.Name = "Button1"
            Me.Button1.TabIndex = 1
            Me.Button1.Text = "Button1"
            '
            'Panel1
            '
            Me.Panel1.Controls.Add(Me.TextBox1)
            Me.Panel1.Location = New System.Drawing.Point(246, 84)
            Me.Panel1.Name = "Panel1"
            Me.Panel1.Size = New System.Drawing.Size(162, 60)
            Me.Panel1.TabIndex = 2
            '
            'Panel2
            '
            Me.Panel2.Location = New System.Drawing.Point(431, 215)
            Me.Panel2.Name = "Panel2"
            Me.Panel2.Size = New System.Drawing.Size(162, 60)
            Me.Panel2.TabIndex = 3
            '
            'Panel3
            '
            Me.Panel3.Controls.Add(Me.Label1)
            Me.Panel3.Controls.Add(Me.TextBox2)
            Me.Panel3.Location = New System.Drawing.Point(532, 54)
            Me.Panel3.Name = "Panel3"
            Me.Panel3.Size = New System.Drawing.Size(162, 60)
            Me.Panel3.TabIndex = 3
            '
            'TextBox1
            '
            Me.TextBox1.BackColor = System.Drawing.Color.Red
            Me.TextBox1.Location = New System.Drawing.Point(29, 25)
            Me.TextBox1.Name = "TextBox1"
            Me.TextBox1.Size = New System.Drawing.Size(33, 20)
            Me.TextBox1.TabIndex = 0
            Me.TextBox1.Text = "TextBox1"
            '
            'Label1
            '
            Me.Label1.Location = New System.Drawing.Point(25, 17)
            Me.Label1.Name = "Label1"
            Me.Label1.Size = New System.Drawing.Size(51, 22)
            Me.Label1.TabIndex = 1
            Me.Label1.Text = "Label1"
            '
            'TextBox2
            '
            Me.TextBox2.BackColor = System.Drawing.Color.Red
            Me.TextBox2.Location = New System.Drawing.Point(36, 32)
            Me.TextBox2.Name = "TextBox2"
            Me.TextBox2.Size = New System.Drawing.Size(33, 20)
            Me.TextBox2.TabIndex = 4
            Me.TextBox2.Text = "TextBox2"
            '
            'Form1
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(855, 511)
            Me.Controls.Add(Me.Panel1)
            Me.Controls.Add(Me.Button1)
            Me.Controls.Add(Me.PictureBox1)
            Me.Controls.Add(Me.Panel3)
            Me.Controls.Add(Me.Panel2)
            Me.Name = "Form1"
            Me.Text = "Form1"
            Me.Panel1.ResumeLayout(False)
            Me.Panel3.ResumeLayout(False)
            Me.ResumeLayout(False)
    
        End Sub
    
    #End Region
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim c As New RedControlTracker
            c.RetRedControl(Me)
            MessageBox.Show("Name of the first Red Control on the form is: " & c.Name & ".  It is located on " & c.Parent)
            MessageBox.Show("Name of the first Red Control on Panel3 is: " & c.GetRedControl(Me.Panel3).Name)
        End Sub
    End Class
    For the Class

    Code:
    Public Class RedControlTracker
        Private mvarName As String
        Private mvarControl As Control
        Private mvarParent As String
        Friend ReadOnly Property Parent() As String
            Get
                Return mvarControl.Parent.Name
            End Get
        End Property
        Friend ReadOnly Property Name() As String
            Get
                Return mvarControl.Name
            End Get
        End Property
        Friend ReadOnly Property ControlObject() As Control
            Get
                Return mvarControl
            End Get
        End Property
    
        Public Sub RetRedControl(ByRef ParentObject As Object)
            Dim c As Control
            For Each c In ParentObject.Controls
                If c.BackColor.Equals(Color.Red) Then
                    mvarControl = c
                    Exit Sub
                End If
            Next
    
            For Each c In ParentObject.Controls
                Select Case c.GetType.ToString.ToUpper
                    Case "System.Windows.Forms.TabControl".ToUpper
                        RetRedControl(c)
                        Exit Sub
                    Case "System.Windows.Forms.TabPage".ToUpper
                        RetRedControl(c)
                        Exit Sub
                    Case "System.Windows.Forms.GroupBox".ToUpper
                        RetRedControl(c)
                        Exit Sub
                    Case "System.Windows.Forms.Panel".ToUpper
                        RetRedControl(c)
                        Exit Sub
                End Select
            Next
            mvarControl = Nothing
        End Sub
    
        Public Function GetRedControl(ByRef ParentObject As Object) As Control
            Dim c As Control
            For Each c In ParentObject.Controls
                If c.BackColor.Equals(Color.Red) Then
                    Return c
                End If
            Next
    
            For Each c In ParentObject.Controls
                Select Case c.GetType.ToString.ToUpper
                    Case "System.Windows.Forms.TabControl".ToUpper
                        Return GetRedControl(c)
                    Case "System.Windows.Forms.TabPage".ToUpper
                        Return GetRedControl(c)
                    Case "System.Windows.Forms.GroupBox".ToUpper
                        Return GetRedControl(c)
                    Case "System.Windows.Forms.Panel".ToUpper
                        Return GetRedControl(c)
                End Select
            Next
            Return Nothing
        End Function
    End Class
    Note, This code will error out if there is no red control on the form. To run, create a new project with Form1. Add a new class. Replace ALL the code in the form with the top Code Section. Replace ALL the code in the class with the bottom code section.

  6. #6
    Join Date
    Dec 2002
    Posts
    305

    Re: Error: Object reference not set to an instance of an object

    Hans:

    I tried the code you have posted after making the changes I suggested. It works as long as you have at least one control with red backcolor. If there is no control with red backcolor, an exception is thrown because c.GetRedControl.Name is not defined. To avoid the exception from being displayed, put the messagebox.show in a try catch end try as follows:

    Try
    Messagebox.Show(("Name of the Red Control is: " & c.GetRedControl.Name)
    catch
    end try

    Good luck.

  7. #7
    Join Date
    Mar 2002
    Posts
    113

    Re: Error: Object reference not set to an instance of an object

    sotoasty,
    Thank you for your examples. Works great!

    Gizmo001,
    Thank you for helping as well. I could not get it to work your way, maybe because the "red" control was created dynamically.

    In any case thatnk you both for your time and effort,
    Hans

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured