- VBForums
- Visual
Basic
- Visual Basic .NET
- [RESOLVED] Object reference not set to an instance of an object
Jul 6th, 2018, 11:07 AM
#1 Thread Starter Hyperactive Member
[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 SubCode: 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 Jul 6th, 2018, 12:14 PM
#2 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 Jul 6th, 2018, 01:45 PM
#3 Thread Starter Hyperactive Member
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 Jul 6th, 2018, 01:51 PM
#4 Re: Object reference not set to an instance of an object Originally Posted by
Chrissy 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. Jul 6th, 2018, 01:59 PM
#5 Thread Starter Hyperactive Member
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? Jul 6th, 2018, 03:21 PM #6 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 = descbecause before you reach that line you have this reference to cmbX, 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. Jul 6th, 2018, 03:29 PM
#7 Thread Starter Hyperactive Member
Re: Object reference not set to an instance of an object Originally Posted by wes4dbt
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. Jul 6th, 2018, 03:36 PM #8 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. Jul 6th, 2018, 03:52 PM
#9 Thread Starter Hyperactive Member
Re: Object reference not set to an instance of an object Originally Posted by wes4dbt
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"?
Jul 6th, 2018, 03:56 PM #10 Re: Object reference not set to an instance of an object Originally Posted by Chrissy Positive that is where the error is. Could it be with the code surrounding "desc"? Well,
what's the value of cmbX and desc? Jul 6th, 2018, 04:01 PM #11 Thread Starter Hyperactive Member 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. Jul 6th, 2018, 04:18 PM
#12 Re: Object reference not set to an instance of an object Originally Posted by
Chrissy 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 Jul 6th, 2018, 04:21 PM #13 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 SubCode: 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 ClassForm Load Event
Code: UpdateTaxProjectionCombobox(Me.ComboBox1) Jul 9th, 2018, 07:37 AM
#14 Thread Starter Hyperactive Member
Re: Object reference not set to an instance of an object Originally Posted by wes4dbt
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???
Jul 9th, 2018, 08:51 AM #15 Thread Starter
Hyperactive Member 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? Jul 9th, 2018, 09:30 AM
#16 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. Jul 9th, 2018, 09:33 AM
#17 Thread Starter Hyperactive Member
Re: Object reference not set to an instance of an object Originally Posted by si_the_geek
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. Jul 9th, 2018, 01:21 PM
#18 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. Jul 9th, 2018, 01:51 PM #19 Thread Starter Hyperactive Member Re: [RESOLVED] Object reference not set to an instance of an object Originally Posted by wes4dbt That's strange that the code execution wasn't stopped in the SelectedIndexChanged event. Glad you found your problem. Agreed... thanks for
your help.
- VBForums
- Visual Basic
- Visual Basic .NET
- [RESOLVED] Object reference not set to an instance of an object
Posting Permissions - You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your
posts
-
- BB code is On
- Smilies are On
- [IMG] code is On
- [VIDEO] code is On
- HTML code is Off
Forum Rules
| Click Here to Expand Forum to Full Width
|