Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NullReferenceException was unhandled

Status
Not open for further replies.

Paco75

Programmer
Oct 11, 2001
239
US
Hi,

I get this error but i dont understand why... here is the code :

Code:
        Try
            conn.Open() 'Open a connection to database
            Dim stm As String = "SELECT * FROM myTable" 
            Dim cmd As MySqlCommand = New MySqlCommand(stm, conn)
            Dim reader As MySqlDataReader = cmd.ExecuteReader()

            While reader.Read()
                 length = Me.convertor.CmToInches(reader.GetString(2))
            End While
        Catch ex As MySqlException
            MsgBox("Error: " & ex.ToString())
        Finally
            conn.Close() 'Close connection to database
        End Try

The error happens on the ... length = Me.convertor.CmToInches(reader.GetString(2)) line but if i place a watch on reader.GetString(2) it is not null. I can see there is a value of a string with numbers like "1234.56" !!!
 
Here is the function called.

Code:
Public Class custConvert
    Public Function CmToInches(ByVal cm As String) As String
        Dim dCm As Decimal = CDec(cm)
        Dim dInches As Decimal = Math.Round(dCm * 0.393701, 3)
        CmToInches = dInches.ToString
    End Function
End Class
 
Well then, the null ref. exception must be coming from something else on the line. Where is 'converter' declared, and is it instantiated before this code is called? Put a watch on that and see if it is what's null.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
I declared my Class custConvert with New and all is ok now. Noob error! Thanks :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top