vb.net - {A first chance exception of type 'System.NullReferenceException' occurred in calculator.exe} error during declaring variable -
how write code in right way?
public class form1 dim y string = lbl_1.text
it says:
{a first chance exception of type 'system.nullreferenceexception' occurred in calculator.exe}
can me guys?
this sample code
public class form1 dim y string = lbl_1.text private sub btn_diff_click(byval sender system.object, byval e system.eventargs) handles btn_diff.click lbl_1.text = y & "-*" end sub private sub btn_1_click(byval sender system.object, byval e system.eventargs) handles btn_1.click lbl_1.text = y & "1" end sub private sub lbl_1_click(byval sender system.object, byval e system.eventargs) handles lbl_1.click dim y string = lbl_1.text lbl_1.text = y end sub private sub btn_n_click(byval sender system.object, byval e system.eventargs) handles btn_n.click lbl_1.text = "" lbl_1.focus() end sub private sub btn_2_click(byval sender system.object, byval e system.eventargs) handles btn_2.click dim y string = lbl_1.text lbl_1.text = y & "2" end sub private sub btn_equal_click(byval sender system.object, byval e system.eventargs) handles btn_equal.click lbl_1.text = val(lbl_1.text) end sub
end class
i want make calculator
but should write in last button (btn_equal)? have tried val doesnt work want
also when declare y in each conrol works in puplic doesnt work
the controls in class form1 need initialized before being used. if want use control in way need explicitly add parameterless constructor form1 class
public class form1 dim y string public sub new() ' call required designer. initializecomponent() ' add initialization after initializecomponent() call. y = lbl_1.text .... end sub end class
in actual code, reading of label control's text happens before label has been created in initializecomponent
hidden call. if declare explicitly parameterless constructor (public sub new()) vs ide adds call initializecomponent
, place initialization of string variable after creation of label.
(you find initializecomponent method inside form1.designer.vb
file if click show files in properties window)
for future knowledge consider read qa cases of nullreferenceexception
discussed thoroughly.
Comments
Post a Comment