Object reference Error keeps coming up

Posted on 2004-12-01
Last Modified: 2010-04-24
I'm writing some code that passes strings between two forms.  At first i was passing them like this:

public user_name as string
public proj_codes() as string

dim frmMain as form1

frmMain.user_name = "test"
frmMain.proj_codes = array

My code would be okay for the "frmMain.user_name = "test"" line but would put up the error

An unhandled exception of type 'System.NullReferenceException' occurred in

Additional information: Object reference not set to an instance of an object.

For the second line.  So i tried to put it in a module and it works.  but now i have a subroutine being called from form2 that is in the module to run a subroutine in form1.  The same error keeps popping up, but this time at the beginning of Form1:  Public Class Form1.  Am i doing anything wrong.  All i want to do is pass some strings from Form2 into Form1 after a button is clicked.  thanks in advance for any help!

Question by:kevinmeredith
    LVL 96

    Expert Comment

    by:Bob Learned
    Dim frmMain as form1 = New form1

    LVL 1

    Accepted Solution


       Or you can declare the variables you want to see in other clases shared:

    public shared user_name as string
    public shared proj_codes() as string

    Shared fields are accessible through the class name:

    FORM 2:
    form1.user_name = "test"
    form1.proj_codes = array

      But you have to be very careful with shared variables.Changing the field value through one instance of your class affects all other instances.



    Author Comment

    The command:

    frmMain as form1 = new Form1

    makes the a new window pop up, so that doesn't work.  

    Haqipmc, your answer works for strings.  But how can i call a subroutine in form1 from form2?  once form2 is completed, i need to update form1.  can this be done the same way?  thanks for your help!

    LVL 1

    Expert Comment


       Yes, kevinmeredith.. shared subs and functions work just like shared fiels (are the same to all the instances of a class)... but i think this is not what you need here.

      If you want to update form1 from form2(or from elsewhere) you should never use close method on form1. Calling this method all resources created within the object are closed and the form is disposed. Use instead Hide and Show methods on form1. This way you have an instance of form1 that can use to update all its public fields or properties.


    Author Comment

    I don't know where you came up with the idea that i use the close method.  I never use.  What i'm doing is opening up a Preferences Form to save some constants.  Whem i'm done with the Prefernces form, i send the data to form1 and close form2.  But i want to start a function that will update all the variables in form one, including some control objects.  When i try to do this, i get the Object Refence Error.  This is my main question.  Thanks for your help btw!

    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    Hi kevinmeredith,

    What you can do is create a class to hold the data to be passed between the forms.  Then, in your Preferences form, declare an event to pass the data back to form1 like this:

    ' --------------------------------------------------------------------------------------

    Public Class PreferencesData
        Public user_name As String
        Public proj_codes() As String
    End Class

    ' --------------------------------------------------------------------------------------

    Public Class Form1 ' Main Form
        Inherits System.Windows.Forms.Form

        Private WithEvents prefs As Form2

        Private Sub btnPreferences_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreferences.Click
            prefs = New Form2
        End Sub

        Private Sub prefs_newPreferences(ByVal pd As PreferencesData) Handles prefs.newPreferences
            ' do something with your new preferences data in here
            Dim s As String
            For Each s In pd.proj_codes
        End Sub
    End Class

    ' --------------------------------------------------------------------------------------

    Public Class Form2 ' Preferences Form
        Inherits System.Windows.Forms.Form

        Public Event newPreferences(ByVal pd As PreferencesData)

        Private pd As New PreferencesData

        Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
            pd.user_name = "some data from the form"
            pd.proj_codes = Split("cat,dog,fish", ",") ' some more data from the form
            RaiseEvent newPreferences(pd)
        End Sub
    End Class

    Author Comment

    That worked Idle_mind.  thanks a bunch!


    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

    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    This video discusses moving either the default database or any database to a new volume.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    732 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

    17 Experts available now in Live!

    Get 1:1 Help Now