Results 1 to 19 of 19

Thread: [RESOLVED] Object reference not set to an instance of an object

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Itasca, IL USA
    Posts
    279

    Resolved [RESOLVED] Object reference not set to an instance of an object

    Hello,

    I am getting an error when trying to update a combox. The error I get is "Object reference not set to an instance of an object". I have to click "OK" a couple of times, then the form loads and actually shows the items in the combobox. Any help is appreciated.

    Thank you!!


    This is in my form load event where I want to update the combobox.

    Code:
    Dim TaxProj As New clsTaxEntity
    TaxProj.UpdateTaxProjectionCombobox(cmbTax)
    Code:
    Public Sub UpdateTaxProjectionCombobox(ByRef cmbX As ComboBox)
            Try
    
                Dim desc As New ArrayList
                Dim rsTax As New clsSQLData
    
                cmbX.Items.Clear()
    
                rsTax.ExecuteQuery("SELECT projection_year , tax_desc , ktax FROM tax_projection ORDER BY Projection_year")
    
                For Each drTax As DataRow In rsTax.sqlDataSet.Tables(0).Rows
                    desc.Add(New clsComboTag(String.Format("{0} {1}", drTax("projection_year").ToString.PadRight(10), drTax("tax_desc")), drTax("ktax").ToString))
                Next
    
                cmbX.DataSource = desc            'This is where I get the error
                cmbX.DisplayMember = "Description"
                cmbX.ValueMember = "Value"
    
                rsTax = Nothing
    
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            End Try
        End Sub
    Code:
    Imports System.Windows.Forms
    Public Class clsComboTag
    
        Private StringDesc As String
        Private ValueID As String
    
        Public Sub New(ByVal desc As String, ByVal value As String)
    
            Me.StringDesc = desc
            Me.ValueID = value
    
        End Sub
    
        Public Property Description() As String
            Get
                Return StringDesc
            End Get
            Set(ByVal value As String)
                StringDesc = value
            End Set
        End Property
    
        Public Property Value() As String
            Get
                Return ValueID
            End Get
            Set(ByVal value As String)
                ValueID = value
            End Set
        End Property
    
    End Class

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

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

    1) Unless you have a really good reason to do so, don't use an ArrayList
    2) There is no good reason to use an ArrayList
    3) List(Of clsComboTag) would have been better
    4) Not that this is your problem, but set the DisplayMember and ValueMember first, then the DataSource

    I don't think any of that solves your problem though.

    When you get the "Object reference not set to an instance of an object" error, it means that something on that line that you're trying to use is Nothing.
    If it's the line that you indicate, there are only three objects on that line:
    cmbX.DataSource = desc
    cmbX, DataSource, and desc. since desc is on the right, it doesn't matter if it's Nothing or not, so we can rule that out. DataSource is a property, so it wouldn't matter if that object is Nothing, in fact Nothing should be its default setting anyways, so we can rule that out. Which leaves cmbX. So, the next time the error happens and the IDE takes you to that line, select cmbX, and then press Ctrl-Shift-F9 (QuickLook, I think thatt's the right keyboard shortcut, also available in a right-click) and you should get a pop up form that tells you what the value of cmbX is... and my guess is that it will be Nothing. If that's the case, then you need to go back through the calling code and find out why cmbX is Nothing... either you forgot to pass something in, or something isn't what it should be.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Itasca, IL USA
    Posts
    279

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

    Maybe I am not coding something right... I am new to VB.NET, most of my experience is in VBA and VB6.

    cmbX is a combobox passed from my load event of the form. The cmbTax is already drawn on the form... can I not pass it byRef to a class? Do I need to do something different with cmbX after it is passed to UpdateTaxProjectionCombobox?

    Code:
    Private Sub frmEntityTemplate_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'This is in my form load event where cmbTax is a combobox that is drawn on my form.
            Dim TaxProj As New clsTaxEntity
            TaxProj.UpdateTaxProjectionCombobox(cmbTax)
    End Sub
    
    
     Public Sub UpdateTaxProjectionCombobox(ByRef cmbX As ComboBox)
            Try
    'I updated the code per your comments.
                Dim desc As New List(Of clsComboTag)
                Dim rsTax As New clsSQLData
    
                cmbX.Items.Clear()
    
                rsTax.ExecuteQuery("SELECT projection_year , tax_desc , ktax FROM tax_projection ORDER BY Projection_year")
    
                For Each drTax As DataRow In rsTax.sqlDataSet.Tables(0).Rows
                    desc.Add(New clsComboTag(String.Format("{0} {1}", drTax("projection_year").ToString.PadRight(10), drTax("tax_desc")), drTax("ktax").ToString))
                Next
    
                cmbX.DisplayMember = "Description"
                cmbX.ValueMember = "Value"
                cmbX.DataSource = desc
    
                rsTax = Nothing
    
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            End Try
        End Sub
    Last edited by si_the_geek; Jul 6th, 2018 at 01:50 PM. Reason: fixed typo in tags

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

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

    Quote Originally Posted by Chrissy View Post
    cmbX is a combobox passed from my load event of the form. The cmbTax is already drawn on the form...
    Actually it might not be at that point, because the Load event happens before the form is drawn - and it is possible that cmbTax has not been initialised at that point.

    Try using the Shown event instead of the Load event.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Itasca, IL USA
    Posts
    279

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

    I still get the same error when moving it to the Shown event.

    I have "Option Explicit On".. could it be something that is not properly cast?

  6. #6
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

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

    I don't see how cmbX is Nothing at this line,
    Code:
    cmbX.DataSource = desc
    because before you reach that line you have this reference to cmbX,
    Code:
    cmbX.Items.Clear()
    If cmbX is Nothing wouldn't this throw an error.

    I'm not sure why your even passing cmbTax, if it's added to the form in the designer then it's accessible to all underlying code in the form.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Itasca, IL USA
    Posts
    279

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

    Quote Originally Posted by wes4dbt View Post

    I'm not sure why your even passing cmbTax, if it's added to the form in the designer then it's accessible to all underlying code in the form.
    The code is not on the form. It is in a separate class.

  8. #8
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

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

    Are you sure where the error occurs? Have you tried commenting out the Try/Catch code, also put a Break Point on that line and check the value of cmbX.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Itasca, IL USA
    Posts
    279

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

    Quote Originally Posted by wes4dbt View Post
    Are you sure where the error occurs? Have you tried commenting out the Try/Catch code, also put a Break Point on that line and check the value of cmbX.
    Positive that is where the error is. Could it be with the code surrounding "desc"?

  10. #10
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

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

    Quote Originally Posted by Chrissy View Post
    Positive that is where the error is. Could it be with the code surrounding "desc"?
    Well, what's the value of cmbX and desc?

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Itasca, IL USA
    Posts
    279

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

    cmbX is a combobox that I am trying to load with an array of the clsComboTag class. All the code is in my original post.

  12. #12
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

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

    Quote Originally Posted by Chrissy View Post
    cmbX is a combobox that I am trying to load with an array of the clsComboTag class. All the code is in my original post.
    I know that, I was asking what values you get when you hit the break point at this line,
    Code:
    cmbX.DataSource = desc

  13. #13
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

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

    btw - This code works fine for me.

    Module1
    Code:
        Public Sub UpdateTaxProjectionCombobox(ByRef cmbX As ComboBox)
            Try
    
                Dim desc As New List(Of ComboBoxTestClass)
    
    
                cmbX.Items.Clear()
    
    
                For i As Integer = 1 To 5
                    desc.Add(New ComboBoxTestClass("desc" & i.ToString, "value" & i.ToString))
                Next
    
    
                cmbX.DisplayMember = "Description"
                cmbX.ValueMember = "Value"
                cmbX.DataSource = desc
    
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
            End Try
        End Sub
    Code:
    Public Class ComboBoxTestClass
    
        Private StringDesc As String
        Private ValueID As String
    
        Public Sub New(ByVal desc As String, ByVal value As String)
    
            Me.StringDesc = desc
            Me.ValueID = value
    
        End Sub
    
        Public Property Description() As String
            Get
                Return StringDesc
            End Get
            Set(ByVal value As String)
                StringDesc = value
            End Set
        End Property
    
        Public Property Value() As String
            Get
                Return ValueID
            End Get
            Set(ByVal value As String)
                ValueID = value
            End Set
        End Property
    End Class
    Form Load Event
    Code:
    UpdateTaxProjectionCombobox(Me.ComboBox1)

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Itasca, IL USA
    Posts
    279

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

    Quote Originally Posted by wes4dbt View Post
    I know that, I was asking what values you get when you hit the break point at this line,
    Code:
    cmbX.DataSource = desc

    When I hover over each part is shows the folloing:

    cmbX = {System.Windows.Forms.ComboBox, Items.Count: 0}

    cmbX.DataSource = Nothing

    desc shows all the values it should.

    Once I execute the line of code, I get the message but the information still populates. Below are the values after the line execution.

    cmbX = {System.Windows.Forms.ComboBox, Items.Count:3}

    cmbX.DataSource = Count: 3 & shows all values it should.

    I am perplexed by this error.... It has to be something simple I am not seeing???

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Itasca, IL USA
    Posts
    279

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

    I figured out why I am getting the error message.

    I have code in the SelectedIndexChanged event for the combobox and am guessing it is being fired when trying to set the datasource of the combobox. I removed the code and didn't get the error.

    How can I keep the code in the SelectedIndexChanged event but prevent from firing when populating the combobox?

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

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

    The SelectedIndexChanged event fires whenever the selected index changes (no matter how that happened), the SelectionChangeCommitted event only fires in response to changes by the user... so simply moving the code from one to the other is likely to fix it.

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Itasca, IL USA
    Posts
    279

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

    Quote Originally Posted by si_the_geek View Post
    The SelectedIndexChanged event fires whenever the selected index changes (no matter how that happened), the SelectionChangeCommitted event only fires in response to changes by the user... so simply moving the code from one to the other is likely to fix it.
    Thank you! Problem solved.

  18. #18
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    Re: [RESOLVED] Object reference not set to an instance of an object

    That's strange that the code execution wasn't stopped in the SelectedIndexChanged event. Glad you found your problem.

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Itasca, IL USA
    Posts
    279

    Re: [RESOLVED] Object reference not set to an instance of an object

    Quote Originally Posted by wes4dbt View Post
    That's strange that the code execution wasn't stopped in the SelectedIndexChanged event. Glad you found your problem.
    Agreed... thanks for your help.

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