Object reference Error keeps coming up

I'm writing some code that passes strings between two forms.  At first i was passing them like this:

FORM1:
public user_name as string
public proj_codes() as string

FORM2:
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!


kevinmeredithAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
Dim frmMain as form1 = New form1

Bob
0
hagipmcCommented:

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

FORM1:
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.

  Cheers!

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kevinmeredithAuthor Commented:
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!

0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

hagipmcCommented:

   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.

 
0
kevinmeredithAuthor Commented:
haqipmc,
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!

0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
        prefs.ShowDialog()
    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
        Debug.WriteLine(pd.user_name)
        For Each s In pd.proj_codes
            Debug.WriteLine(s)
        Next
    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)
        Me.Close()
    End Sub
End Class
0
kevinmeredithAuthor Commented:
That worked Idle_mind.  thanks a bunch!

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

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.