Object reference not set to an instance of an object runtime error

  • Thread starter Thread starter JoshuaAT123
  • Start date Start date
J

JoshuaAT123

Guest
Hello. I'm trying to run this code but for some reason I keep getting the run time error:

'Object reference not set to an instance of an object'

Box_extra.Form1.PictureBox1.get returned nothing


It happens at this line:

Dim bmp As New Bitmap(PictureBox1.ClientSize.Width, PictureBox1.ClientSize.Height)


Rest of code below:

Public Class Form1

Private Class LineX
Public Pt1 As PointF
Public Pt2 As PointF
End Class

Private Lines As New List(Of LineX)

Private MouseStage As Integer

Private MouseDownPt, MouseMovePt As PointF

' Dim g As System.Drawing.Graphics
Dim r As System.Drawing.Graphics
Dim a As System.Drawing.Graphics


Dim col As Color
Dim D As Integer = 2
Dim p As Pen = New Pen(col, D)


Dim q As Pen = New Pen(Color.Black, 0)
Dim t As Pen = New Pen(Color.Blue, 5)
Dim n As Pen = New Pen(Color.Black, 5)
Dim count As Integer = 0
Dim trac As Integer = 1
Dim H, W As Integer
Dim point1 As New Point()
Dim point2 As New Point()
Dim point3 As New Point()

Dim myPointsArray As New List(Of PointF)
Dim myPointsArrays(100)

Dim ShowGrid As Boolean = False
Dim Draw As Boolean = False
Dim TurnOnDrawL As Boolean = False
Dim StopConD As Boolean = False
Dim Snapo As Boolean = False
Dim Pushed As Boolean = True
Dim truethat As Boolean = True


Dim isDrag As Boolean = False
Dim theRectangle As New Rectangle(New Point(0, 0), New Size(0, 0))
Dim startPoint As Point

'draw on a bitmap for pictuebox 2
'create a memory bitmap and graphics surface
Dim bmp As New Bitmap(PictureBox1.ClientSize.Width, PictureBox1.ClientSize.Height)
Dim g As Graphics = Graphics.FromImage(bmp)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load


Me.Text = "Drawing Application Version 1.0"

End Sub




Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint



End Sub

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click



D = Depth.Text
W = Width.Text
H = Height.Text

p.Color = col
p.Width = D



Lineop.Enabled = True

ResetButton.Enabled = True

Button1.Enabled = False



'draw the vector graphics on the bitmap via the drawing surface graphics object
RenderScene(PictureBox1.CreateGraphics)

'reference the bitmap in the picturebox
PictureBox1.Image = bmp

'to do: dispose the image as required






End Sub

Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles Red.CheckedChanged


col = Color.Red
p.Color = col

End Sub


Private Sub White_CheckedChanged(sender As Object, e As EventArgs) Handles White.CheckedChanged

col = Color.White
p.Color = col

End Sub

Private Sub Blue_CheckedChanged(sender As Object, e As EventArgs) Handles Blue.CheckedChanged

col = Color.Blue
p.Color = col

End Sub

Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
p.DashStyle = Drawing2D.DashStyle.Solid
End Sub

Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
p.DashStyle = Drawing2D.DashStyle.Dash
End Sub

Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged

Draw = CheckBox1.Checked


End Sub

Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick

MouseMovePt = e.Location

Select Case MouseStage
Case 0
If e.Button = MouseButtons.Left Then
'start drawing
MouseDownPt = e.Location
MouseStage = 1
End If

Case Else 'right mouse button cancel drawing
If e.Button <> MouseButtons.Left Then
MouseStage = 0
PictureBox1.Invalidate()
End If
End Select


End Sub


Private Sub About_Click(sender As Object, e As EventArgs) Handles About.Click

MessageBox.Show("Hi, my name is Joshua. Check out unpopularthemovie.com (It has a Bible message). Thanks for using this application. All rights, privaleges, and royalties are for Joshua Tashbar(maker of this application). Copyright created in 2018",
"About", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation)


End Sub


Private Sub gridon_Checked(sender As Object, e As EventArgs) Handles gridon.CheckedChanged

ShowGrid = gridon.Checked

If ShowGrid = True Then
q.Color = Color.Black
Else
q.Color = Color.LightGray
End If




End Sub



Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove

If MouseStage = 1 Then
MouseMovePt = e.Location
PictureBox1.Invalidate()
End If


End Sub

Private Sub ResetButton_Click(sender As Object, e As EventArgs) Handles ResetButton.Click

PictureBox1.Refresh()


End Sub

'Button to save picture
Private Sub SavePicture_Click(sender As Object, e As EventArgs) Handles SavePicture.Click

Dim SaveDLG As New SaveFileDialog
bmp = New Drawing.Bitmap(878, 735)
Dim GFX As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmp)



'g.CopyFromScreen(222, 60, 222, 60, bmp.Size)



SaveDLG.Filter = "JPEG Files (*.jpeg)|*.jpeg|All Files|*.*"

SaveDLG.Title = "Save File As"
SaveDLG.InitialDirectory = "C:\"

DialogResult = SaveDLG.ShowDialog

If DialogResult = Windows.Forms.DialogResult.OK Then



'PictureBox1.Image = bmp




'PictureBox1.DrawToBitmap(bmp, PictureBox1.DisplayRectangle)

bmp.Save(SaveDLG.FileName)



bmp.Save("Drawing.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)

'Trying out Copyfromscreen
'g.CopyFromScreen(PictureBox1.Width, PictureBox1.Height)


ElseIf DialogResult = Windows.Forms.DialogResult.Cancel Then
End If



End Sub


Private Sub BStop_Click(sender As Object, e As EventArgs)

StopConD = True


End Sub

Private Sub Snapon_CheckedChanged(sender As Object, e As EventArgs) Handles Snapon.CheckedChanged


Snapo = Snapon.Checked

End Sub

Private Sub Lineop_Click(sender As Object, e As EventArgs) Handles Lineop.Click

While trac = 1



If trac = 1 Then
btnun.Enabled = True

btnre.Enabled = True

lblmessage.Text = "Exit New Line Drawing"


End If

trac = trac + 1

End While

If trac = 3 Then
btnun.Enabled = False

btnre.Enabled = False

lblmessage.Text = "..."

trac = 0

End If

trac = trac + 1

End Sub

Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp

If MouseStage = 1 Then
MouseMovePt = e.Location
Dim lx As New LineX
With lx
.Pt1 = MouseDownPt
.Pt2 = MouseMovePt
End With

Lines.Add(lx)

MouseDownPt = MouseMovePt

PictureBox1.Invalidate()
End If

End Sub

Public Sub RenderScene(g As Graphics)
With g
.Clear(Color.White)
.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias

For i As Integer = 1 To 44
g.DrawLine(q, 50 * i, 0, 50 * i, 735)
Next

For j As Integer = 1 To 37
g.DrawLine(q, 0, 50 * j, 878, 50 * j)
Next



g.DrawRectangle(p, 100, 30, 60 * W, 60 * H)

g.DrawRectangle(p, 100, 450, 60 * W, 60 * H)

g.DrawRectangle(p, 588, 449, 60 * W, 60 * H)

g.DrawLine(n, 500, 0, 500, 900)

g.DrawLine(n, 0, 313, 878, 313)


Using p As New Pen(Color.LightBlue, 2)
'draw all lines
For Each lx As LineX In Lines
.DrawLine(p, lx.Pt1, lx.Pt2)
Next

'draw the tracer if drawing
If MouseStage = 1 Then
p.DashStyle = Drawing2D.DashStyle.Dash
p.Color = Color.Yellow
.DrawLine(p, MouseDownPt, MouseMovePt)
End If

End Using



End With
End Sub

End Class

Continue reading...
 
Back
Top