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

Friends,

Hmmm...I'm not sure what's wrong here.  I am getting the Object reference not set to an instance of an object error, and I'm not sure what I need to do different.  I've used, almost, the exact same routine elsewhere, just as it is, with success

I have a combobox, called cmbEventList, and I want to update it, from another form.  Simply trying the cmbEventList.Text = NewEventName (String), doesn't work (no error, but it doesn't update it).  So I decided to go the Public Property route.  Now, the only problem is, using it, I get the aforementioned error.

Here is my main form:

    Private Sub SCMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        frmUpdateEventName.SCMainClass = Me
    End Sub

    Private Sub btnUpdateEventNameRU_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdateEventNameRU.Click
        Dim UpdateEventName As New frmUpdateEventName
        UpdateEventName.Visible = True
    End Sub

Public Class frmUpdateEventName

    Public mySCMain As SCMain
    Public Property SCMainClass() As SCMain
        Get
            SCMainClass = mySCMain
        End Get
        Set(ByVal Value As SCMain)
            mySCMain = Value
        End Set
    End Property
    Dim tableToUpdate As String = Nothing

    Private Sub UpdateEventName_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            txtOldEventName.Text = SCMain.cmbEventName.Text()
            txtNewEventName.Text = SCMain.cmbEventName.Text()
 
    End Sub

    Private Sub btnUpdateEventNameNow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdateEventNameNow.Click
        Dim dbUpdate As New databaseInteractions_Update
        Dim NewEventName As String = txtNewEventName.Text()

        dbUpdate.UpdateDatabase_String(SCMain.RUdsn, tableToUpdate, "EventName", NewEventName, "WHERE EventName = '" & txtOldEventName.Text() & "'")
        mySCMain.cmbEventName.Text = NewEventName '<--------- It dies here
        Me.Close()
    End Sub
End Class

Is somebody seeing something I'm not, or am I missing something.

Thanks in advance!

Eric

indy500fanAsked:
Who is Participating?
 
riyazthadConnect With a Mentor Commented:
Try with DropDown
0
 
riyazthadCommented:
Try this

change  line

Public mySCMain As SCMain

to

 Public mySCMain As New SCMain
0
 
indy500fanAuthor Commented:
riyazthad,

Yeah, I tried that, and it eliminated the error; however, the mySCMain.cmbEventList.Text is not getting updated.

Any other suggestions?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
riyazthadCommented:
You created a new instance mySCMain of Type SCMain. I dont know where u created this code.

Now u have new form instance. You set combo value also. But think that, u r chaning current instance , not parent form.

If u want to see your changes, u have to look to new instance form.

means

just try this

mySCMain.cmbEventName.Text = NewEventName '<--------- It dies here
mySCMain.Show() '/ you will get new instace and ur changed combo
Me.Close()

Now you understood what is going on , and change logic accordingly.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
This suggests that you are using VB.Net 2005:

    Private Sub SCMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        frmUpdateEventName.SCMainClass = Me
    End Sub

Is this the case?

If so, then just use the forms name to reference it.

That is unless you are creating new instances of the form and showing it.

As in...

    Dim scm As New SCMain
    scm.Show()

Versus:

    SCMain.Show()  ' <-- without ever using "New"

Which version are you working with and how are you showing the forms?
0
 
riyazthadCommented:
Creating an instance of form and updating parent form's combo box is bad method.

you can do it by using delegate and event handling. In your second form create a event and set this event to one of Sub in your parent form. you can set your text value to combo box by firing that event. You can get textbox value in parent form thru event parameter System.Object.

I hope it will help u.
0
 
indy500fanAuthor Commented:
Hey guys, I've been away from my PC for a while, sorry it's taken so long for me to respond.

Let me try a couple of your suggested solutions, and I'll bet back later today.  riyazthad, I think your suggestion of using an event, makes the most sense.

Thanks!

Eric
0
 
riyazthadCommented:
Eric,

If you are using Vs2005 , I am agreeing with Idle_Mind 100%. It is easy to do this stuffs in 2005 now.

if it is 2003, u can try various method, like Event handling etc.

0
 
indy500fanAuthor Commented:
Okay everyone.  I'm back in the office and I've tried a couple of things.

Here are my results:

Idle_Mind, yes, you would be correct, I am using VS2005.

riyazthad,

Either method of setting the SCMain.cmbEventName.Text = NewEventName or using events, neither one updates my mainform's combobox text value on the screen.  I've tried, cmbEventName.Text and cmbEventName.SelectedText, but neither seems to update it.  Any other suggestions?
0
 
riyazthadCommented:
What are in combobox? Is there any values? Other than newly assigning text?

If you dropdownstyle is DropDownList both Text and SelectedText will work. Try SelctedIndex instead.

U can try just know how it is working, u can try with SCMain.Text = "Me changed Form heading" , test any changes happened? If so, then SelectedIndex will help u.

or just try put any number like

 SCMain.cmbEventName.SelectedIndex = 2

(I hope there are ferw items in combo)
0
 
indy500fanAuthor Commented:
The number of Items in the combobox are variable.  It is based on a qry that populates the list.

Now, my routine actually updates the value in the database, where the qry gets the eventlist, and then afterword I is when I was wanting to update the cmb.Text.  So, with that in mind, do you still think SelectedIndex will still work???

There are two routines associated with the combobox and they both happen before I go and update the .text value:

    Private Sub cmbEventName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbEventName.SelectedIndexChanged
        Dim SCEventName As String = cmbEventName.Text
        SCEventName = SCIPSEventName

        GetSeriesEventInfo("OCEvent", SCEventName)

    End Sub

    Private Sub cmbIRLEventName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbEventName.DropDown

        Try

            cmbEventName.Items.Clear()
            GetEventList("OCEvent")

        Catch ex As Exception
            MessageBox.Show(ex.Message, "SQL Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try


    End Sub

I'll try your suggestion>

0
 
riyazthadCommented:
It looks like u have a combo box with all event names in form1. From  Form2 you are selecting one of event (I assume not adding new event). So you now index or primary ley, or item unique number of each event name. So better to use selectedIndex property. That will help u to select an event name.

Could u update form caption? (Just for test)
0
 
indy500fanAuthor Commented:
riyazthad,

Sorry, using either method, yes the form caption does change to NewTextValue.

Yes, I was creating a new event, not using either of the existing events.
0
 
indy500fanAuthor Commented:
riyazthad,

So, What Syntax do I need  to make this work if I have the SelectedIndex and the new text value?

 Public Sub GoUpdate_cmb(ByRef NewTextValue As String, ByRef cmbboxSelectedIndex As Integer) Handles UpdateEventName.UpdatecmbBox

        cmbIRLEventName.SelectedText() = NewTextValue

    End Sub
0
 
riyazthadCommented:
What is your DropDownDown style property?
0
 
indy500fanAuthor Commented:
DropDownList
0
 
indy500fanAuthor Commented:
That did it!  Thank you very much!
0
 
riyazthadCommented:
you are welcome

;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.