Learn how to a build a cloud-first strategyRegister Now


Object reference Error keeps coming up

Posted on 2004-12-01
Medium Priority
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 system.windows.forms.dll

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
ID: 12720456
Dim frmMain as form1 = New form1


Accepted Solution

hagipmc earned 300 total points
ID: 12723834

   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:

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

ID: 12730268
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!

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 12733879

   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

ID: 12736222
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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 500 total points
ID: 12736321
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

ID: 12737549
That worked Idle_mind.  thanks a bunch!


Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month20 days, 17 hours left to enroll

810 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