Problem executing a FOR loop

MichiKen

New member
Joined
Feb 1, 2011
Messages
1
Programming Experience
Beginner
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:

VB.NET:
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
 
i sorted out the first part of the program so it allows you to enter more the 1 name, however after entering the 5th name you can still type so there is still something wrong with it. Here is what i have done so far


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


hope this helps
 
Your For loop runs for 5 iterations and you have Console.ReadLine inside the loop, so you'll be reading 5 lines. You then have Console.ReadLine after the loop too, so you'll be reading a 6th line.

Also, what is your 'SortedList' variable supposed to be? Whatever it is, how about you actually say so in your code?

Finally, you're assigning the result of ReadLine to the same variable each time, therefore overwriting whatever you put there last time.
 
Back
Top