Solved

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

Posted on 2006-12-01
18
205 Views
Last Modified: 2010-04-23
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

0
Comment
Question by:indy500fan
  • 9
  • 8
18 Comments
 
LVL 6

Expert Comment

by:riyazthad
ID: 18056844
Try this

change  line

Public mySCMain As SCMain

to

 Public mySCMain As New SCMain
0
 

Author Comment

by:indy500fan
ID: 18056858
riyazthad,

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

Any other suggestions?
0
 
LVL 6

Expert Comment

by:riyazthad
ID: 18056955
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18057246
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
 
LVL 6

Expert Comment

by:riyazthad
ID: 18061486
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
 

Author Comment

by:indy500fan
ID: 18063259
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
 
LVL 6

Expert Comment

by:riyazthad
ID: 18068750
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
 

Author Comment

by:indy500fan
ID: 18068774
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
 
LVL 6

Expert Comment

by:riyazthad
ID: 18068924
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:indy500fan
ID: 18068969
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
 
LVL 6

Expert Comment

by:riyazthad
ID: 18069073
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
 

Author Comment

by:indy500fan
ID: 18069121
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
 

Author Comment

by:indy500fan
ID: 18069212
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
 
LVL 6

Expert Comment

by:riyazthad
ID: 18069218
What is your DropDownDown style property?
0
 

Author Comment

by:indy500fan
ID: 18069225
DropDownList
0
 
LVL 6

Accepted Solution

by:
riyazthad earned 500 total points
ID: 18069248
Try with DropDown
0
 

Author Comment

by:indy500fan
ID: 18069269
That did it!  Thank you very much!
0
 
LVL 6

Expert Comment

by:riyazthad
ID: 18069280
you are welcome

;-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now