Locked Why do I keep getting this error? VB 2010 Express

  • Tuesday, July 17, 2012 2:27 PM
     
     

    This is what I have, but I get an error when I try debugging this and then it highlights bmp.Save(sPath) in yellow as if it doesn't know what I'm talking about, if someone could come up with a solution to my problem, and maybe some organization tips, that would be great ;)

    Public Class Form1

        Private Function MakeName(ByVal Pathname As String, ByVal Count As Integer) As String
            Return System.IO.Path.Combine(Pathname, "ss" & Count.ToString & ".bmp")

        End Function

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Me.Opacity = 0
            If My.Computer.FileSystem.FileExists("C:\cache") Then
            Else
                My.Computer.FileSystem.CreateDirectory("C:\cache")
            End If
            Dim SaveCount As Integer = 0
            Dim sPath As String
            Dim bmp As Bitmap
            Timer1.Enabled = True
            Do
                sPath = MakeName("C:\cache", SaveCount)
                SaveCount += 1
            Loop Until Not System.IO.File.Exists(sPath)
            bmp = CType(PictureBox1.Image, Bitmap)
            bmp.Save(sPath)

        End Sub

        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim bounds As Rectangle
            Dim screenshot As System.Drawing.Bitmap
            Dim graph As Graphics
            bounds = Screen.PrimaryScreen.Bounds
            screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
            graph = Graphics.FromImage(screenshot)
            graph.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
            PictureBox1.Image = screenshot
            Timer1.Enabled = False
            Me.Opacity = 100

        End Sub

    End Class

    I tried to start debugging, I clicked button1 and it highlighted bmp.Save(sPath) and said:

    NullReferenceException was unhandled

    Object reference not set to an instance of an object.



    • Edited by Daniel l3 Tuesday, July 17, 2012 3:11 PM
    •  

All Replies

  • Tuesday, July 17, 2012 2:50 PM
     
     

    "I get an error"  What does this mean? What if you continue pressing the shortcut for single-step?

    BTW, you should dispose the Graphics object after usage. And I suggest to directly call IO.Directory.Create.


    Armin

  • Tuesday, July 17, 2012 3:19 PM
     
     

    "I get an error"  What does this mean? What if you continue pressing the shortcut for single-step?

    BTW, you should dispose the Graphics object after usage. And I suggest to directly call IO.Directory.Create.


    Armin

    I've changed my post a little, it should be easier to understand what "error" I'm getting.
  • Tuesday, July 17, 2012 3:38 PM
     
     Answered

    Thx for clarifying.

    Probably the timer hasn't ticked yet and there's no image in the picturebox. Is that right? If so, set the button's enabled property to false in the designer and set it to true at run-time in the timer's tick event.

    EDIT: I just see "Timer1.Enabled = True" in the button click event. The timer won't tick before the click event handler has exitted. Why don't you start the timer before?


    Armin


    • Edited by Armin Zingler Tuesday, July 17, 2012 3:40 PM
    • Marked As Answer by Daniel l3 Tuesday, July 17, 2012 4:26 PM
    •