0

An unhandled exception of type 'System.NullReferenceException' occurred in sql.exe

Additional information: Object reference not set to an instance of an object.

Dim imageData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
If Not imageData Is Nothing Then
        Using m1 As New MemoryStream(imageData, 0, imageData.Length)
            m1.Write(imageData, 0, imageData.Length)
            PictureBox1.BackgroundImage = Image.FromStream(m1, True)
        End Using
end if 
LarsTech
  • 77,282
  • 14
  • 135
  • 204
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – LarsTech Nov 07 '14 at 00:19

1 Answers1

0

You don't have enough code posted to have a positive way to identify the line. You should go into the debug menu, exceptions, and set the break on CLR/framework errors. That will halt on the exact line where the error is occurring when running from code.

cmd might be NULL
Image might be NULL
It's possible, but less likely that PictureBox1 is NULL.

It's probably better to add checks to ensure those objects are not NULL so you can build a more robust method.

UnhandledExcepSean
  • 11,653
  • 2
  • 32
  • 49