0

How to write this code in a right way?

Public Class Form1
   Dim y As String = lbl_1.Text

It says:

{A first chance exception of type 'System.NullReferenceException' occurred in calculator.exe}

Can you help me guys?

this is a sample from the code

Public Class Form1
Dim y As String = lbl_1.Text

Private Sub btn_diff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_diff.Click

    lbl_1.Text = y & "-*"
End Sub


Private Sub btn_1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_1.Click

    lbl_1.Text = y & "1"
End Sub

Private Sub lbl_1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbl_1.Click
    Dim y As String = lbl_1.Text
    lbl_1.Text = y
End Sub

Private Sub btn_n_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_n.Click
    lbl_1.Text = ""
    lbl_1.Focus()
End Sub

Private Sub btn_2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_2.Click
    Dim y As String = lbl_1.Text
    lbl_1.Text = y & "2"
End Sub

Private Sub btn_equal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_equal.Click
    lbl_1.Text = Val(lbl_1.Text)
End Sub

End Class

i want to make a calculator

but what should i write in the last button (btn_equal)? i have tried val but it doesnt work as i want

also when i declare y in each conrol it works but in puplic it doesnt work

  • 4
    you are trying to access an object which is not initialised. post your full code including where the error occurs. – Ahmed ilyas Dec 16 '14 at 13:25
  • If you assign `lbl_1.Text` to that variable it keeps it's value(probably `String.Empty`) even if `lbl_1.Text` is changed later. So if you access `y` later you don't get the current value of `lbl_1.Text`. Why do you need that variable at all? – Tim Schmelter Dec 16 '14 at 13:31
  • lbl_1 is initialized in form's constructor. Looks like it is not yet initialized when you are trying to access it. So, lbl_1.Text throws an error because lbl_1 is null and Text property could not be accessed. – Artemix Dec 16 '14 at 13:36
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Dec 16 '14 at 13:46
  • @Plutonix Yes, in the end it all comes down to what is said there, but I think that here we have something else that deserves an explanation .. – Steve Dec 16 '14 at 13:48
  • I think the longest explanation involves this aspect (See **Visual Basic Forms**) – Ňɏssa Pøngjǣrdenlarp Dec 16 '14 at 13:50
  • I HAVE TRIED to declare the variable in each control and it works but it doesn`t work when its declared in puplic and i dont know what is the problem –  Dec 16 '14 at 18:04

1 Answers1

4

The controls in the class Form1 need to be initialized before being used. If you want to use a control in that way you need to explicitly add the parameterless constructor to Form1 class

Public Class Form1

    Dim y as String

    Public Sub New()


       ' This call is required by the designer.
       InitializeComponent()

       ' Add any initialization after the InitializeComponent() call.
       y = lbl_1.Text
       ....
   End Sub

End Class

In your actual code, the reading of the label control's text happens before the label itself has been created in the InitializeComponent hidden call. If you declare explicitly the parameterless constructor (Public Sub New()) then the VS IDE adds the call to the InitializeComponent and you could place the initialization of your string variable after the creation of the label.
(You could find the InitializeComponent method inside the Form1.Designer.vb file if you click Show All Files in the properties window)

For future knowledge consider to read this QA where cases of NullReferenceException are discussed thoroughly.

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
  • iam still a beginner in the visual basic i cant understand (instalize component) can u Explain more easily –  Dec 16 '14 at 18:08
  • When you place a control over a form, there is some (not truly) hidden code at play. This code is responsable to declare the control, initialize it, set the properties of the control according to what you have set in the form designer, link the control events to your event handler code and finally add the control into the Form controls collection. This happens in the InitializeComponent method contained in the FormName.Designer.vb file. It is all there in clear text, just look at it. – Steve Dec 16 '14 at 18:13
  • In C# this infrastructure is always visible because the IDE always create the parameterless form constructor for you and show it. In VB.NET instead the parameterless constructor is not showed automatically but is nevertheless called when you create the form in code. If you manually add the _Public Sub New()_ then the IDE shows all the details – Steve Dec 16 '14 at 18:19
  • it worked the error dont pop up any more but it doesnt work as iwant the code in buttons is lbl_1.text=y&"say 3" but it show only 3 –  Dec 16 '14 at 18:47
  • No prob @MohamedKhaled it is just for clarity. Being you a new user of the site you will discover that this site works in a different way than a forum. Future readers should be able to find an answer to specific questions without following an evolution of the question and the answer. So it is a common habit to call this pattern in that way. I did not mean to be offensive. See you again on SO – Steve Dec 16 '14 at 21:38