Null reference exceptions  
Author Message
martinabooker





PostPosted: Visual Basic Express Edition, Null reference exceptions Top

Hi People,

I've been programming for a few years now and am still plagued with Null reference exceptions cropping up.
I've nearly always managed to get round them by playing about with my code but never properly understanding HOW I've fixed it and why it works but didn't earlier...

Can someone please explain in noob words why the following code returns a null reference exception :-

Dim g As Graphics
Dim text As String
Dim font As New Font("Arial", 6, FontStyle.Bold, GraphicsUnit.Pixel)
Dim returnValue As SizeF
text = "abcdefg"
returnValue = g.MeasureString(text, font)

Many thanks in advance


Visual Studio Express Editions26  
 
 
Ken Tucker





PostPosted: Visual Basic Express Edition, Null reference exceptions Top

You never created g.

Dim g as graphics = me.creategraphics


 
 
martinabooker





PostPosted: Visual Basic Express Edition, Null reference exceptions Top

That was it !!!
Thankyou Ken - Cant believe I missed it