borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Next Topic (Need some help) New Topic New Poll Post Reply
AndreaVB Forum : VB General : Object reference not set to an instance of an object.
Poster Message
kobiyashi
Level: Trainee

Registered: 15-11-2007
Posts: 1

icon Object reference not set to an instance of an object.

This is probably some extremely silly error on my part, as they usually are. Also this problem it probably way below the knowledge level of you guys but I need to get this done. It's a simple app that's supposed to find how many vowels there are in words from a text file. I get the topic error at the line
letter = word.Substring(x, 1)
When I comment out stars = "", stars has a value of quite a few *'s which makes me think this error happens rather late in the process. Can you guys help me out?

Public Class Form1

    Private Sub thelastbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles thelastbutton.Click
        Dim file As IO.StreamReader = IO.File.OpenText("c:\words2.txt")
        Dim word As String
        Dim letter As String = ""
        Dim x As Integer
        Dim stars As String = ""
        Do
            x = 1
            stars = ""
            word = file.ReadLine
            Do
                letter = word.Substring(x, 1)
                If letter.ToLower = "a" Or letter.ToLower = "e" Or letter.ToLower = "i" Or letter.ToLower = "o" Or letter.ToLower = "u" Then
                    stars = stars + "*"
                End If
                x = x + 1
            Loop Until x = word.Length
            lastlist.Items.Add(stars)
        Loop Until word = ""
        file.Close()
    End Sub
End Class

15-11-2007 at 06:51 PM
View Profile Send Email to User Show All Posts | Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 904
icon Re: Object reference not set to an instance of an object.

You're not checking for end of file.
Word may be null, and you are attempting to use it.
Check for null before you attempt to check the letters.
Kieron


____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

21-11-2007 at 07:56 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB General : Object reference not set to an instance of an object.
Next Topic (Need some help) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder