-1

I'm a beginner so please be nice. I'm making a currency exchange calculator in Visual Basic and I've come across an error that I can't seem to figure out:

  • Object reference not set to an instance of an object.

Here's the code below that I'm struggling with:

Dim Currency As Double
Dim TextOther As String = Othertext.Text
Dim TextGBP As String = GBPtext.Text

Private Sub Calculate_Click(sender As Object, e As EventArgs) Handles Calculate.Click
    TextOther = Currency * Convert.ToDouble(TextGBP)
    TextGBP = Currency * Convert.ToDouble(TextOther)
End Sub

Thank you in advance.

Blubbs
  • 1
  • In addition to what @Ando said, the reason you're getting a `NullReferenceException` is because, at the point where you declare the variables `TextOther` and `TextGBP`, the form hasn't actually been created. and trying to assign them the .text value of the Textboxes (which don't exist at that point) will result in a null reference exception. – David Wilson Apr 16 '18 at 13:01
  • Oh btw, you might find it usefel to get into the habit of standard naming conventions - Have a look at this - https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/naming-guidelines – David Wilson Apr 16 '18 at 13:04

1 Answers1

1

You need to set your currency to something and use the '.text. after (what I assume is) your textboxes.

Also set your variables in the click event.

Dim Currency As Double
Dim TextOther As String
Dim TextGBP As String

Private Sub Calculate_Click(sender As Object, e As EventArgs) Handles Calculate.Click
currency = 10.0
TextOther = Othertext.Text
TextGBP = GBPtext.Text

TextOther.text = Currency * Convert.ToDouble(TextGBP)
TextGBP.text = Currency * Convert.ToDouble(TextOther)
End Sub
Ando
  • 121
  • 7