I keep getting a System.StackOverflowException error. Anyone have any idea why??

Imports System.IO
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Drawing
Public Class Form1
Inherits System.Windows.Forms.Form

Dim mst As MasterForm = New MasterForm
Dim objColl As StringCollection
Dim objColl2 As StringCollection
Dim objRand As Random = New Random
Dim word As String
Dim int As Integer
Dim cnt As Integer = 0
Dim graf As Graphics
Dim blackPen As New Pen(Color.Black, 3)

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents txtWord As System.Windows.Forms.TextBox
Friend WithEvents txtGuess As System.Windows.Forms.TextBox
Friend WithEvents btnAddNewWord As System.Windows.Forms.Button
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents btnTips As System.Windows.Forms.Button
Friend WithEvents btnExit As System.Windows.Forms.Button
Friend WithEvents btnNewGame As System.Windows.Forms.Button
Friend WithEvents lblOutput As System.Windows.Forms.Label
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents rdbStickMan As System.Windows.Forms.RadioButton
Friend WithEvents rdbPicture As System.Windows.Forms.RadioButton
Friend WithEvents lblBodyParts As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.txtWord = New System.Windows.Forms.TextBox
Me.btnAddNewWord = New System.Windows.Forms.Button
Me.txtGuess = New System.Windows.Forms.TextBox
Me.Label2 = New System.Windows.Forms.Label
Me.btnTips = New System.Windows.Forms.Button
Me.btnExit = New System.Windows.Forms.Button
Me.btnNewGame = New System.Windows.Forms.Button
Me.lblOutput = New System.Windows.Forms.Label
Me.Panel1 = New System.Windows.Forms.Panel
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.rdbStickMan = New System.Windows.Forms.RadioButton
Me.rdbPicture = New System.Windows.Forms.RadioButton
Me.lblBodyParts = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(40, 8)
Me.Label1.Name = "Label1"
Me.Label1.TabIndex = 0
Me.Label1.Text = "The Word:"
'
'txtWord
'
Me.txtWord.Cursor = System.Windows.Forms.Cursors.IBeam
Me.txtWord.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.txtWord.ForeColor = System.Drawing.Color.ForestGreen
Me.txtWord.Location = New System.Drawing.Point(40, 32)
Me.txtWord.Name = "txtWord"
Me.txtWord.ReadOnly = True
Me.txtWord.Size = New System.Drawing.Size(232, 26)
Me.txtWord.TabIndex = 2
Me.txtWord.Text = ""
'
'btnAddNewWord
'
Me.btnAddNewWord.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnAddNewWord.Location = New System.Drawing.Point(528, 232)
Me.btnAddNewWord.Name = "btnAddNewWord"
Me.btnAddNewWord.Size = New System.Drawing.Size(96, 24)
Me.btnAddNewWord.TabIndex = 3
Me.btnAddNewWord.Text = "Edit the list"
'
'txtGuess
'
Me.txtGuess.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.txtGuess.Location = New System.Drawing.Point(144, 384)
Me.txtGuess.MaxLength = 1
Me.txtGuess.Name = "txtGuess"
Me.txtGuess.Size = New System.Drawing.Size(24, 26)
Me.txtGuess.TabIndex = 1
Me.txtGuess.Text = ""
'
'Label2
'
Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label2.Location = New System.Drawing.Point(40, 384)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(96, 23)
Me.Label2.TabIndex = 5
Me.Label2.Text = "Guess a Letter:"
'
'btnTips
'
Me.btnTips.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnTips.Location = New System.Drawing.Point(528, 176)
Me.btnTips.Name = "btnTips"
Me.btnTips.Size = New System.Drawing.Size(96, 23)
Me.btnTips.TabIndex = 6
Me.btnTips.Text = "Tips"
'
'btnExit
'
Me.btnExit.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnExit.Location = New System.Drawing.Point(528, 296)
Me.btnExit.Name = "btnExit"
Me.btnExit.Size = New System.Drawing.Size(96, 23)
Me.btnExit.TabIndex = 7
Me.btnExit.Text = "Exit"
'
'btnNewGame
'
Me.btnNewGame.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnNewGame.Location = New System.Drawing.Point(528, 120)
Me.btnNewGame.Name = "btnNewGame"
Me.btnNewGame.Size = New System.Drawing.Size(96, 23)
Me.btnNewGame.TabIndex = 8
Me.btnNewGame.Text = "New Game"
'
'lblOutput
'
Me.lblOutput.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.lblOutput.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblOutput.ForeColor = System.Drawing.Color.Firebrick
Me.lblOutput.Location = New System.Drawing.Point(216, 368)
Me.lblOutput.Name = "lblOutput"
Me.lblOutput.Size = New System.Drawing.Size(408, 32)
Me.lblOutput.TabIndex = 9
'
'Panel1
'
Me.Panel1.Location = New System.Drawing.Point(40, 101)
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(232, 248)
Me.Panel1.TabIndex = 10
'
'PictureBox1
'
Me.PictureBox1.Location = New System.Drawing.Point(304, 110)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(208, 240)
Me.PictureBox1.TabIndex = 11
Me.PictureBox1.TabStop = False
'
'rdbStickMan
'
Me.rdbStickMan.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.rdbStickMan.Location = New System.Drawing.Point(104, 72)
Me.rdbStickMan.Name = "rdbStickMan"
Me.rdbStickMan.TabIndex = 12
Me.rdbStickMan.Text = "Stick Man"
'
'rdbPicture
'
Me.rdbPicture.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.rdbPicture.Location = New System.Drawing.Point(360, 73)
Me.rdbPicture.Name = "rdbPicture"
Me.rdbPicture.TabIndex = 13
Me.rdbPicture.Text = "Picture"
'
'lblBodyParts
'
Me.lblBodyParts.Location = New System.Drawing.Point(528, 8)
Me.lblBodyParts.Name = "lblBodyParts"
Me.lblBodyParts.Size = New System.Drawing.Size(100, 104)
Me.lblBodyParts.TabIndex = 14
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(640, 422)
Me.Controls.Add(Me.lblBodyParts)
Me.Controls.Add(Me.rdbPicture)
Me.Controls.Add(Me.rdbStickMan)
Me.Controls.Add(Me.PictureBox1)
Me.Controls.Add(Me.Panel1)
Me.Controls.Add(Me.lblOutput)
Me.Controls.Add(Me.btnNewGame)
Me.Controls.Add(Me.btnExit)
Me.Controls.Add(Me.btnTips)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.txtGuess)
Me.Controls.Add(Me.btnAddNewWord)
Me.Controls.Add(Me.txtWord)
Me.Controls.Add(Me.Label1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim objRe As StreamReader = New StreamReader("..\newWords.txt")
Dim line As String = ""
objColl = New StringCollection
'Dim word As String

Dim i As Integer

'line = objRe.ReadLine()

txtWord.Focus()

Do
line = objRe.ReadLine()
If Not line = "" Then
objColl.Add(line)
End If
'txtWord.Text = txtWord.Text & line & ControlChars.NewLine

Loop Until line Is Nothing

objRe.Close()

int = objRand.Next(0, objColl.Count)
word = objColl.Item(int)
'MessageBox.Show(word)
objColl2 = New StringCollection
For i = 0 To word.Length - 1
objColl2.Add("- ")
txtWord.Text &= objColl2.Item(i)

If word.Chars(i) = " " Then
objColl2.Item(i) = " "
End If

Next

'For i = 0 To objColl2.Count - 1
' txtWord.Text &= objColl2.Item(i)
'Next


End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNewWord.Click
mst.ShowDialog(Me)

End Sub

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

Private Sub txtGuess_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtGuess.TextChanged
Dim i As Integer
Dim bl As Boolean = False


'If txtGuess.Text Then

'End If

lblOutput.Text &= txtGuess.Text & " "
txtWord.Clear()
For i = 0 To word.Length - 1
If txtGuess.Text = word.Chars(i) Then
objColl2.Item(i) = txtGuess.Text
bl = True
End If

Next

For i = 0 To objColl2.Count - 1
txtWord.Text &= objColl2.Item(i)

If txtWord.Text = word Then
lblOutput.Text = "You win"
End If
Next
txtGuess.SelectAll()


'Dim strArray As String() = New String() {"head", "nick", "body", "rHand", "lHand", "rLeg", "lLeg", "rope"}
'Dim j As Integer
'Dim bodyPart As String
'Dim b1 As Boolean = False

'For j = 0 To 7
' If bl = False Then
' bodyPart = CStr(strArray.GetValue(j))
' lblBodyParts.Text = bodyPart & vbCrLf

' End If
' j += 1
'Next

If bl = False Then
cnt += 1
'DrawLineOnForm(graf, blackPen)
Panel1.Refresh()
If cnt = 8 Then
lblOutput.Text = "You lose"
End If
End If

'Dim ee As PaintEventArgs
'Panel1.Show(DrawLineOnForm(ee))


End Sub
'Private Sub DrawLineOnForm(ByVal e As PaintEventArgs)
' Dim point1 As New Point(140, 200)
' Dim point2 As New Point(500, 100)
' 'graph.DrawLine(penColor, 68, 10, 30, 40)
' e.Graphics.DrawLine(blackPen, point1, point2)
'End Sub

Private Sub btnNewGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewGame.Click
cnt = 0
Dim i As Integer
txtWord.Focus()
lblOutput.Text = ""
txtGuess.Clear()
txtWord.Clear()
int = objRand.Next(0, objColl.Count)
word = objColl.Item(int)
'MessageBox.Show(word)
objColl2 = New StringCollection
For i = 0 To word.Length - 1
objColl2.Add("?")
Next

For i = 0 To objColl2.Count - 1
txtWord.Text &= objColl2.Item(i)
Next
End Sub
End Class

try debugging with breakpoints and stepping thru the whole program.
a "System.StackOverflowException" error is trying to set a value in an array higher then its length

Do
line = objRe.ReadLine()
If Not line = "" Then
objColl.Add(line)
End If
'txtWord.Text = txtWord.Text & line & ControlChars.NewLine

Loop Until line Is Nothing

line will never be nothing so will keep looping indefinately hence the stack overflow

no that is correct, because he should read the file until the it reaches null which is the termination for a text file

My mistake... rusty vb code knowledge with the Nothing keyword.
However i think i found his overflow problem. The randomizer is set to vary between 0 and the collection count. Shouldnt it be the collection count -1? As the next line is using the randomizer's result to index the collection

its strange because i ran this code without any problems

but your right that is probably the reason why it shows the error and that would account for why i didnt get the error because i never hit that random number

This article has been dead for over six months. Start a new discussion instead.