-1

I am getting a an Null reference exception when trying to add an item to a listbox in a different form.

This is my error at run time.

An unhandled exception of type 'System.NullReferenceException' occurred in ... Additional information: Object reference not set to an instance of an object.

I am trying to connect the Mainform by initializing it at the top of the class of the secondForm. after I have my data i want to add it to a listbox it the mainform.

Public Class FormHairdresser //The second form
     Dim varMainForm As FormMain //connecting the forms ?

Private Sub btnAddHairdresser_Click(sender As Object, e As EventArgs) Handles btnAddHairdresser.Click

hairdresser = HairdresserChoices(HairdresserID) // get the data
  varMainForm.lstListBox.Items.Add(hairdresser) //Run time error breaks here.
DigitalDulphin
  • 55
  • 2
  • 11

4 Answers4

2

All i had to to was write the form name instead of ininalised variable.

FormMain.lstListbox.Items.Add("item")

Instead of

Dim varMainForm As FormMain varMainForm.ListBox.Items.Add("item")

DigitalDulphin
  • 55
  • 2
  • 11
1

You cannot simply create a new instance of your main form (as has been suggested and expect that to work, you need an actual reference to the mainform that you have created. To help you see the logic involved;

Create a new Winforms project. In the default Form1 add a textbox and a button. Now add a new form to this application (you can leave it with its default name of Form2. To this form add a TextBox (call it myTextBox) and a button.

Now go back to your first form and doubleclick the button to access the click handler in code. Add the following:

Dim frm as New Form2
frm.Show

Press f5 and click the button and you'll see a new form 2. So far so good.

Now open up the code for Form2 and add the following code so that it ends up looking like this:

    Public Class Form2

    Private frm As Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        frm.TextBox1.Text = myTextBox.text
    End Sub

End Class

build, press f5 and click the button on form1, in the new form2 enter some text in the text box and click the button, you get your null reference exception. The reason you get this is because at the moment the private field frm inForm2 refers to Nothing.

Now open up the code in Form2 and add a constructor and the following code so that it ends up looking like this:

  Public Class Form2



      Private frm As Form1
        Public Sub New(byval frm1 As Form1)
            'first we should make sure that we have a parameter to play with
            If Not IsNothing(frm1) Then
                frm = DirectCast(frm1,Form1)
            End If
        End Sub



        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            frm.TextBox1.Text = myTextBox.text
        End Sub

    End Class

Finally go back to your first forms buttonclick handler and change the code slightly so that it looks like this;

   Dim frm as New Form2(Me)

frm.Show

Build and run your application, now when you enter text into the textbox in form2 and click the button it will appear in the textbox in Form1.

The reason why this happens is because you have passed an actual reference to the form1 that was originally created when the application started to form2. By casting that reference to your private field used to represent form1 in form2 you can then use it to properly refer to things on form1. This is a very simple concept but one which you need to learn before you will make progress programming.

Dom Sinclair
  • 2,310
  • 1
  • 25
  • 33
0

the Problem is with your initialization of the formmain.with out proper initialization the object you are creating is nothing other than Null.To avoid this we use New Operator.The New operator can often be used to create the instance when you declare it. So the initialization will look like

 Dim varMainForm As New FormMain 

Hope this Helps.For more Reference Object Initialization Errors

update:

 Dim varMainForm As FormMain //connecting the forms ?

Private Sub btnAddHairdresser_Click(sender As Object, e As EventArgs) Handles                      btnAddHairdresser.Click

hairdresser = HairdresserChoices(HairdresserID) // get the data
  varMainForm = New FormMain 
  varMainForm.lstListBox.Items.Add(hairdresser) //Run time error breaks here.
Community
  • 1
  • 1
akhil kumar
  • 1,475
  • 1
  • 11
  • 23
0

Try This.

Public Class FormHairdresser //The second form

        Private Sub btnAddHairdresser_Click(sender As Object, e As EventArgs) Handles btnAddHairdresser.Click

        Dim varMainForm As FormMain 
        hairdresser = HairdresserChoices(HairdresserID) 
        varMainForm.lstListBox.Items.Add(hairdresser) 
E.Solicito
  • 91
  • 9
  • I get a compile time error saying " 'varMainForm' is used before it is assigned a value. A null reference exception could result at run time " under "varMainForm".listListBox.Items.Add(hairdresser) – DigitalDulphin Nov 23 '15 at 05:37
  • what variable type is your hairdresser? it is a list? – E.Solicito Nov 23 '15 at 06:00