|
-
Jan 15th, 2011, 09:59 PM
#1
Thread Starter
Junior Member
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!
-
Jan 16th, 2011, 12:19 AM
#2
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
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Jan 16th, 2011, 01:07 PM
#3
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.
-
Jan 16th, 2011, 01:52 PM
#4
Thread Starter
Junior Member
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!
-
Jan 16th, 2011, 05:11 PM
#5
Thread Starter
Junior Member
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
-
Jan 16th, 2011, 06:34 PM
#6
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.
-
Jan 16th, 2011, 06:44 PM
#7
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?
-
Jan 16th, 2011, 07:03 PM
#8
Thread Starter
Junior Member
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
-
Jan 16th, 2011, 07:22 PM
#9
Re: Null Reference Code in My School Project
 Originally Posted by SlickWilly
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:
Dim condition As Boolean
If condition Then
' This code is executed if condition is true
Else
' This code is executed if condition is false
End If
' This code is executed whether condition is true or false
-
Jan 16th, 2011, 08:00 PM
#10
Thread Starter
Junior Member
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
-
Jan 16th, 2011, 09:47 PM
#11
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.
-
Jan 16th, 2011, 09:53 PM
#12
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?
-
Jan 16th, 2011, 10:24 PM
#13
Thread Starter
Junior Member
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!
-
Jan 16th, 2011, 11:07 PM
#14
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.
-
Jan 17th, 2011, 10:14 PM
#15
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|