Problem Drawing String

brix_zx2

Member
Joined
Apr 6, 2005
Messages
12
Location
Altus AFB
Programming Experience
1-3
vb.net 2k3:

I have this code that doesn't work unless I take out the g.DrawString statements. If anyone can look over the code and tell me why I'd appreciate it. The Error is Object reference not set to an instance of an object.

VB.NET:
Dim g As Graphics
Dim LabelText As String
LabelText = "--"
x = x + 20
Dim varSlash As String
varSlash = "/"
 
 
Dim brushColor As System.Drawing.Color
Dim blackBrush = New SolidBrush(SystemColors.WindowText)
Dim blueBrush = New SolidBrush(brushColor.Blue)
Dim greenBrush = New SolidBrush(brushColor.Green)
Dim redBrush = New SolidBrush(brushColor.Red)
Dim yellowBrush = New SolidBrush(brushColor.Yellow)
Dim grayBrush = New SolidBrush(brushColor.Gray)
Dim labelFont As New Font("Arial", 40)
Dim format As New StringFormat
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Center
 
y = 200
g.DrawString(varSection.ToString, Font, blackBrush, x, y)
y = y + 15
g.DrawString(LabelText.ToString, Font, blackBrush, x, y)
y = y + 5
g.DrawString(varCheckList.ToString, Font, blackBrush, x, y)
y = y + 50
g.DrawString(varValidated.ToString, Font, blueBrush, x, y)
y = y + 10
g.DrawString(varSlash.ToString, Font, blackBrush, x, y)
y = y + 10
g.DrawString(varAwaiting.ToString, Font, greenBrush, x, y)
y = y + 10
g.DrawString(varSlash.ToString, Font, blackBrush, x, y)
y = y + 10
g.DrawString(varNot.ToString, Font, redBrush, x, y)
y = y + 10
g.DrawString(varSlash.ToString, Font, blackBrush, x, y)
y = y + 10
g.DrawString(varFollow.ToString, Font, yellowBrush, x, y)
y = y + 10
g.DrawString(varSlash.ToString, Font, blackBrush, x, y)
y = y + 10
g.DrawString(varNA.ToString, Font, grayBrush, x, y)
 
Dim g As Graphics = something.creategraphics
where 'something' is an object. The form, a button etc
 
In this type of situation, you need to determine which reference has not been set to an instance. Click the Break button on the Unhandled Exception dialog, press F10 to step the code, then test each variable by mousing over it or using the Locals or Watch window. Doing this would have told you that your Graphics object had not been instantiated, which TPM has already kindly told you how to do.
 
Back
Top