-1
    Dim graph As Graphics = Nothing
    Dim frmleft As System.Drawing.Point = Me.Bounds.Location
    Dim bmp As New Bitmap(Me.Bounds.Width + 8, Me.Bounds.Height + 8)
    graph = Graphics.FromImage(bmp)
    Dim screenx As Integer = frmleft.X
    Dim screeny As Integer = frmleft.Y
    graph.CopyFromScreen(screenx - 5, screeny - 5, 0, 0, bmp.Size)
    PictureBox3.BackgroundImageLayout = ImageLayout.Stretch
    PictureBox3.BackgroundImage = bmp


    If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
        PictureBox3.Image.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)   'Error Here!!!!!!'

    End If

End Sub

This code is meant to screen shot and save it at the same time.The program does screen shot said form but the saving part is where the error lies.

1 Answers1

0

This existing Q&A is tagged vb.net, does it not help?

What is a NullReferenceException, and how do I fix it?

Debugging

How do you find the source of a NullReferenceException? Apart from looking at the exception itself, which will be thrown exactly at the location where it occurs, the general rules of debugging in Visual Studio apply: place strategic breakpoints and inspect your variables, either by hovering the mouse over their names, opening a (Quick)Watch window or using the various debugging panels like Locals and Autos.

If you want to find out where the reference is or isn't set, right-click its name and select "Find All References". You can then place a breakpoint at every found location and run your program with the debugger attached. Every time the debugger breaks on such a breakpoint, you need to determine whether you expect the reference to be non-null, inspect the variable and and verify that it points to an instance when you expect it to.

By following the program flow this way, you can find the location where the instance should not be null, and why it isn't properly set.

I find these instructions to be language-agnostic.

Community
  • 1
  • 1
Shaun Wilson
  • 8,320
  • 3
  • 46
  • 46
  • very helpful. anger much? i usually link to resources and then fill out answers over time. in this case it's entirely an attempt to provide direction. i'm sure someone else with the same rep or more will come along shortly to vote to close this as a duplicate, they may or may not bother to provide any help other than a link. there are also codereview and programmers exchanges on the stack network. i do think the existing answers will help him. – Shaun Wilson Jan 18 '18 at 02:57