Solved

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

Posted on 2006-12-01
18
206 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

10 Experts available now in Live!

Get 1:1 Help Now