Click to See Complete Forum and Search --> : Problem with a FOR Loop in VB.NET


MichiKen
01-30-2011, 05:25 PM
I'm new to VB.NET. Just trying to run a basic program where the user is prompted to enter five names, one at a time. I'm prompted one time. After entering one name I get the following error "Unhandled exception: System.NullReferenceException:Object reference not set to an instance of an object"

Here's the code:



Module Module1

Sub Main()
Dim SortedList
For SortedList = 1 To 5
Console.WriteLine("Enter a favorite name (you'll be prompted 5 times): ")
SortedList = Console.ReadLine()
Next
Dim myItem As DictionaryEntry
For Each myItem In SortedList
Console.WriteLine(myItem.Value)
Next
End Sub

End Module

Laserson
02-01-2011, 07:34 AM
Maybe because you are using the same variable for loop body and reading console input? After first iteration your loop counter will have entered value from console (name (string), not integer).