Exception: object reference not set to an instance of an object

brspnr
01-04-2005, 07:38 PM
Hi,
i have 2 form and i want to clear items of listbox on myform1 from myform2, by clicking button on myform2.

i added this code to myform2

Public frm1 As Form1
Public Sub New(ByVal frm As Form1)
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
frm1 = frm


End Sub

and then
i called listbox and i tried to clear it from myform2
frm1.listboxone.items.clear()

but i got an exception .
"System.NullReferenceException "
"object reference not set to an instance of an object "

What should i do ?

AFterlife
01-04-2005, 08:35 PM
Pass the listbox to the constructor instead of the form.

AtmaWeapon
01-05-2005, 08:12 AM
Where is the code you are using to call this constructor? Are you sure you are declaring a form instance before passing it?

brspnr
01-05-2005, 04:05 PM
i tried but i couldnt access the listbox on form1
Pass the listbox to the constructor instead of the form.

brspnr
01-05-2005, 04:10 PM
The code is on myform2 and i declared form instance before passing it



Where is the code you are using to call this constructor? Are you sure you are declaring a form instance before passing it?

sgt_pinky
01-05-2005, 04:21 PM
AtmaWeapon means show us how you have instantiated your Form2.

Did you do this?


Dim frm2 as New Form2(Me)
frm2.Show()

brspnr
01-05-2005, 04:38 PM
ok..
i wrote this code on myform1
here my code:

Dim frm as new form2
frm.ShowDialog()






AtmaWeapon means show us how you have instantiated your Form2.

Did you do this?


Dim frm2 as New Form2(Me)
frm2.Show()

sgt_pinky
01-05-2005, 05:17 PM
Ok, thats the problem. Your 'Public Sub New(ByVal frm As Form1)' in Form2 is expecting a variable of type Form1 to create it.

There should have been a compiler error thrown when you tried to run it though, if this is the case.

Regardless, you must declare your new Form2 as I did above.

Then you set your variable frm1 to the passed Form1 variable, just as you did originally, and everything should work.

brspnr
01-07-2005, 07:54 AM
Thank u so much...




Ok, thats the problem. Your 'Public Sub New(ByVal frm As Form1)' in Form2 is expecting a variable of type Form1 to create it.

There should have been a compiler error thrown when you tried to run it though, if this is the case.

Regardless, you must declare your new Form2 as I did above.

Then you set your variable frm1 to the passed Form1 variable, just as you did originally, and everything should work.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum