Results 1 to 15 of 15

Thread: Null Reference Code in My School Project

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    18

    Cool Null Reference Code in My School Project

    Hello! I am basically 4 weeks into my first ever programming class and we are working on a project for the game "Hangman". While I have struggled with everything for the past 3 weeks, I have finally cams across something I have no idea how to correct. I am getting a Null.Reference error in the following line of my code:
    If Not strWordToGuess.Contains(strLetterGuessed) Then

    Now this code was provided in my project files, it was dated 4/07, I am currently working in VB 8 express, I am thinking this might have something to do with it.

    Any help anyone could provide, or shine some light onto this dark area would be greatly appreciated. Thanks!

  2. #2
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Null Reference Code in My School Project

    How does the relation of strLetterGuessed work in relation to the rest of the code? You should debug it and step through the code to see what strLetterGuessed contains, through out the process.

    More than likely, that code is being called before strLetterGuessed is anything, but without seeing more code, I can't be sure.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Null Reference Code in My School Project

    My guess is that it's strWordToGuess that's not been initilized to any value, if it's Nothing then you would get a null reference exception since you can't call the Contains method on Nothing. Make sure you initilize strWordToGuess to an empty string.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    18

    Re: Null Reference Code in My School Project

    This is the message I get when I debug:
    A first chance exception of type 'System.NullReferenceException' occurred in IT180ProjectStart.exe

    I Have also encountered another problem in btnGuessWord field. When you type in the correct word the first time the program tells you, you are incorrect, push the guess word button again, it pops up as though you guessed the correct word, and then tells you you guessed the word in 2 tries, then I get another message box the says you are incorrect, you have tried 2 times. Here is my complete code, I have tagged where my problems are. Yes I know this is MY homework, I am not asking for anyone to do it, just some basic guidence. As I have been in this class for 4 weeks and have learned more from the internet then what I have been taught in class. Code is as follows:

    Public Class frmMain
    Dim strLetterGuessed As String
    Dim strWordGuessed As String
    Dim strWordToGuess As String
    Dim strLettersGuessed As String = "B,A,S,S,E,T,T, H,O,U,N,D,"
    Dim intNumTries As Integer = 0
    Dim intNumWrongTries As Integer



    'Copy code from SampleCode.txt here during Part 3
    Private Sub GuessLetter()
    Dim intLetterIndex As Integer = 0

    intNumTries += 1
    'THIS IS WHERE I AM GETTING THE NULL.REFERENCE ERROR CODE
    If Not strWordToGuess.Contains(strLetterGuessed) Then
    'END
    intNumWrongTries += 1

    If intNumWrongTries = 6 Then
    MessageBox.Show("You lose")
    lblWord.Text = strWordToGuess

    Else
    MessageBox.Show("That is not correct. You have guessed wrong " & intNumWrongTries & " times.")
    End If

    Else
    strWordGuessed = lblWord.Text
    intLetterIndex = strWordToGuess.IndexOf(strLetterGuessed)

    While intLetterIndex <> -1 'No more instances of the letter

    If intLetterIndex <> -1 Then
    strWordGuessed = strWordGuessed.Remove(intLetterIndex, 1) 'Remove the 'X'
    strWordGuessed = strWordGuessed.Insert(intLetterIndex, strLetterGuessed)
    End If
    intLetterIndex = strWordToGuess.IndexOf(strLetterGuessed, intLetterIndex + 1)
    End While

    lblWord.Text = strWordGuessed


    If lblWord.Text = strWordToGuess Then
    MessageBox.Show("You guessed the word in " & intNumTries & " tries.")
    End If

    End If

    End Sub




    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub picBody_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picBody.Click

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    Me.Close()

    End Sub

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
    strWordToGuess = "BASSETT HOUND"
    intNumTries = 0
    intNumWrongTries = 0

    End Sub

    Private Sub btnSolve_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSolve.Click
    lblWord.Text = "Bassett Hound"
    End Sub

    Private Sub btnGuessL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuessL.Click
    strLetterGuessed = txtLetter.Text
    intNumTries += 1
    Call GuessLetter()
    End Sub

    Private Sub lblGuessed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblGuessed.Click

    End Sub

    'THIS IS WHERE I KNOW MY PROBLEM IS, BUT I JUST CAN'T SEEM TO FIGURE OUT WHERE MY CODING IS WRONG
    Private Sub btnGuessW_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuessW.Click
    Dim strWordGuessed = "BASSETT HOUND"
    Dim strWordtoGuess = "BASSETT HOUND"
    strWordGuessed = txtWord.Text()
    txtWord.Text = strWordGuessed.ToUpper()
    intNumTries += 1
    If strWordGuessed = strWordtoGuess Then
    MessageBox.Show("BASSETT HOUND")
    MessageBox.Show("You guessed the word in " & intNumTries & " tries.")
    End If
    intNumWrongTries = intNumWrongTries + 1
    If intNumWrongTries < 6 Then
    MessageBox.Show("That is not correct. You have guessed wrong " & intNumWrongTries & " times.")
    ElseIf intNumWrongTries = 6 Then
    MessageBox.Show("You lose")
    lblWord.Text = "Bassett Hound"

    End If
    End Sub

    Private Sub txtLetter_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLetter.TextChanged

    End Sub
    End Class

    Any help with the coding would be greatly appreciated. Thanks!

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    18

    Exclamation Re: Null Reference Code in My School Project

    If someone could please help I would appreciate it. My assignment is do in tonight by midnight. Thanks, Bill

  6. #6
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Null Reference Code in My School Project

    Perhaps it might help to look at how you've laid out your control structures:

    Code:
    intNumTries += 1
    If strWordGuessed = strWordtoGuess Then
        MessageBox.Show("BASSETT HOUND")
        MessageBox.Show("You guessed the word in " & intNumTries & " tries.")
    End If
    intNumWrongTries = intNumWrongTries + 1
    If intNumWrongTries < 6 Then
        MessageBox.Show("That is not correct. You have guessed wrong " & intNumWrongTries & " times.")
    ElseIf intNumWrongTries = 6 Then
        MessageBox.Show("You lose")
        lblWord.Text = "Bassett Hound"
    End If
    So what your code will do:

    Firstly, increment the count of tries.
    Secondly, if player guessed correctly, tell them so and how many tries it took.
    Thirdly, increment the count of incorrect tries
    Fourthly, if the number of incorrect tries is less than 6, tell the player they guessed wrong, or if the number of incorrect tries is equal to 6, tell the player they lose.

    When put in those terms, can you spot the second error (the incorrectly telling the player they've guessed wrong)? Remember that a computer program will be executed quite literally by the computer, step by step.

  7. #7
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Null Reference Code in My School Project

    As to your first error, let's look at what the first part your code is doing:

    Code:
    Dim strWordGuessed = "BASSETT HOUND"
    Dim strWordtoGuess = "BASSETT HOUND"
    strWordGuessed = txtWord.Text()
    txtWord.Text = strWordGuessed.ToUpper()
    Now, I can see that you are entering the word "bassett hound" into the text box.
    Let's examine what happens after each step in the variables:

    Initially:
    txtWord.Text = "bassett hound"
    strWordGuessed = uninitialised
    strWordtoGuess = uninitialised

    Code:
    Dim strWordGuessed = "BASSETT HOUND"
    Dim strWordtoGuess = "BASSETT HOUND"
    txtWord.Text = "bassett hound"
    strWordGuessed = "BASSETT HOUND"
    strWordtoGuess = "BASSETT HOUND"

    Code:
    strWordGuessed = txtWord.Text()
    txtWord.Text = "bassett hound"
    strWordGuessed = "bassett hound"
    strWordtoGuess = "BASSETT HOUND"

    Code:
    txtWord.Text = strWordGuessed.ToUpper()
    txtWord.Text = "BASSETT HOUND"
    strWordGuessed = "bassett hound"
    strWordtoGuess = "BASSETT HOUND"

    Now, you compare whether strWordGuessed and strWordtoGuess are the same. The way you've done your comparison is case-sensitive. Therefore "BASSETT HOUND" and "bassett hound" are different strings.

    Now, the second time through, initially:
    txtWord.Text = "BASSETT HOUND"
    strWordGuessed = uninitialised
    strWordtoGuess = uninitialised

    Note that the text in the textbox is now upper case. What difference does that make once you get to the comparison?

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    18

    Re: Null Reference Code in My School Project

    I am assuming this is the code you are refering to in telling them they have guessed wrong:

    If intNumWrongTries < 6 Then
    MessageBox.Show("That is not correct. You have guessed wrong " & intNumWrongTries & " times.")
    ElseIf intNumWrongTries = 6 Then
    But if they guess the correct word, should not my End If statement stop there?

    Thx, Bill

  9. #9
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Null Reference Code in My School Project

    Quote Originally Posted by SlickWilly View Post
    But if they guess the correct word, should not my End If statement stop there?
    No. The code inside the If block is executed only if the condition is true. Outside the block is executed regardless of the condition.

    You are probably thinking of an Else clause:

    vbnet Code:
    1. Dim condition As Boolean
    2.  
    3. If condition Then
    4.     ' This code is executed if condition is true
    5. Else
    6.     ' This code is executed if condition is false
    7. End If
    8. ' This code is executed whether condition is true or false

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    18

    Re: Null Reference Code in My School Project

    Ok, I kinda understand what you are saying! Yes I know this is probably simple, but I am just learning. I have added you suggestions in quotations in my text. Thanks, Bill

    Private Sub btnGuessW_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuessW.Click
    Dim strWordGuessed = "BASSETT HOUND"
    Dim strWordtoGuess = "BASSETT HOUND"
    "Dim condition As Boolean"
    "If strWordGuessed = txtWord.Text() Then"
    If txtWord.Text = strWordGuessed.ToUpper()
    "ELSE" intNumTries += 1
    If strWordGuessed = strWordtoGuess Then
    MessageBox.Show("BASSETT HOUND")
    MessageBox.Show("You guessed the word in " & intNumTries & " tries.")
    End If
    intNumWrongTries = intNumWrongTries + 1
    If intNumWrongTries < 6 Then
    MessageBox.Show("That is not correct. You have guessed wrong " & intNumWrongTries & " times.")
    ElseIf intNumWrongTries = 6 Then
    MessageBox.Show("You lose")
    lblWord.Text = "Bassett Hound"

    End If
    End Sub

  11. #11
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Null Reference Code in My School Project

    I'm not quite sure what you've done there. Let's take a step back and think about this rather than plowing ahead piling more code in.

    We want to do the following if we guess correctly:
    Code:
    MessageBox.Show("BASSETT HOUND")
    MessageBox.Show("You guessed the word in " & intNumTries & " tries.")
    And otherwise we want to do the following:
    Code:
    intNumWrongTries = intNumWrongTries + 1
    If intNumWrongTries < 6 Then
        MessageBox.Show("That is not correct. You have guessed wrong " & intNumWrongTries & " times.")
    ElseIf intNumWrongTries = 6 Then
        MessageBox.Show("You lose")
        lblWord.Text = "Bassett Hound"
    End If
    The fact that you've coded up an If/ElseIf block correctly suggests you shouldn't have any problem coding up an If/Else block with these two pieces of code in them.

  12. #12
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Null Reference Code in My School Project

    Also think about:
    1) We want to increment the intNumTries variable whether the guess was correct or not.
    2) We're still not dealing with case-sensitivity at all well. Perhaps we should investigate the many mechanisms for comparing strings to see if there is a better way?

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    18

    Re: Null Reference Code in My School Project

    Ok, evil I feel more confused than ever. Here I thought I was catching on a little bit! If you don't mind, can you just mark my error areas,Please don't fix them! just mark where you are tlking about, I feel like I am spinning out of control. Also just to let you know, you have taught me more in a few hours then I have learned in 4 weeks attending this school! Thanks for your help!

  14. #14
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Null Reference Code in My School Project

    Okay, let's go back to your original sub. I'll mark in Red the condition you're interested in (whether the words match), Blue the code that should be executed if the condition is true and Green the code that should execute if the condition is false:

    Code:
    Private Sub btnGuessW_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuessW.Click
        Dim strWordGuessed = "BASSETT HOUND"
        Dim strWordtoGuess = "BASSETT HOUND"
        strWordGuessed = txtWord.Text()
        txtWord.Text = strWordGuessed.ToUpper()
        intNumTries += 1
        If strWordGuessed = strWordtoGuess Then
            MessageBox.Show("BASSETT HOUND")
            MessageBox.Show("You guessed the word in " & intNumTries & " tries.")
        End If
        intNumWrongTries = intNumWrongTries + 1
        If intNumWrongTries < 6 Then
            MessageBox.Show("That is not correct. You have guessed wrong " & intNumWrongTries & " times.")
        ElseIf intNumWrongTries = 6 Then
            MessageBox.Show("You lose")
            lblWord.Text = "Bassett Hound"
    
        End If
    End Sub
    And compare that to what an If/Else block should look like:

    Code:
    If conditionExpression Then
        ' This code executed if condition true
    Else
        ' This code executed if condition false
    End If
    Last edited by Evil_Giraffe; Jan 16th, 2011 at 11:13 PM.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    18

    Thumbs up RESOLVED Null Reference Code in My School Project

    Thanks for everyones help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width