0

I have had a big seemingly unsolvable problem when coding a guessing game. I am getting an exception error on the line:

MessageLabel.Text = Message.ToString

Here is the code:

Option Strict On

Public Class ABabyGuess

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim randomGenerator As New Random
        Dim secretNumber As Integer
        Const NUMBER_MIN As Integer = 1
        Const NUMBER_MAX As Integer = 200
        Dim guessNumber As Integer
        Dim Won As Boolean = False
        Dim Message As String = ""
        Dim MAX_TRIES As Integer = 500
        Dim CurrentTrys As Integer
        secretNumber = randomGenerator.Next(NUMBER_MIN, NUMBER_MAX)
        Try
            Do While CurrentTrys <= MAX_TRIES And Not Won
                CurrentTrys = CurrentTrys + 1
                ABaby.ATryTableLabel.Text = ""
                guessNumber = Integer.Parse(GuessTextBox.Text)
                If guessNumber > secretNumber Then
                    Message = Message & guessNumber.ToString & "Too Big" & Environment.NewLine
                ElseIf guessNumber < secretNumber Then
                    Message = Message & guessNumber.ToString & "Too Small" & Environment.NewLine
                ElseIf guessNumber = secretNumber Then
                    Won = True
                End If
            Loop
        Catch ex As Exception
            If guessNumber > 100 Then
                MessageBox.Show("Please inut a number below 100.")
            ElseIf guessNumber = 0 Then
                MessageBox.Show("Please input a number above 0")
            End If
        End Try
            If Message.ToString IsNot Nothing Then
                ABaby.ATryLabel.Text = Message.ToString
            End If
        If Won = True Then
            MessageBox.Show("congratulations! You Won!", "congratulations", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        ElseIf Won = False And CurrentTrys = 7 Then
            MessageBox.Show("...You actually lost. I give up...", "...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If
    End Sub
End Class

The error is NullReferenceException was Unhandled with details saying: {"Object variable or With block variable not set."} I honestly could not find anywhere on the internet to solve this problem. And if you were wondering why I called the form ABaby, it's the easiest difficulty level available in my multi-difficulty project.

WolfLink
  • 3,308
  • 2
  • 26
  • 44
  • Welcome to Stack Overflow! Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Apr 19 '13 at 04:06

1 Answers1

0

It is because you either have not initialized MessageLabel or Message or Both.

Check your code to see if they are properly initialized before calling or accessing their methods/ properties.

Black Maggie
  • 496
  • 2
  • 15