2014-11-07 42 views
0

類型「System.NullReferenceException」的未處理的異常發生在sql.exe類型「System.NullReferenceException」的未處理的異常發生在sql.exe

其他信息:對象沒有設置爲一個對象的一個​​實例。

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 
+0

可能重複[什麼是NullReferenceException,我該如何解決它?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-doi-i-fix-它) – LarsTech 2014-11-07 00:19:33

回答

0

您不必貼有一個積極的方式來識別夠用就行的代碼。您應該進入調試菜單,例外,並設置CLR /框架錯誤的中斷。這將停止在從代碼運行時發生錯誤的確切行上。

CMD可能是NULL
圖片可能是NULL
這是可能的,但不太可能PictureBox1爲NULL。

最好添加檢查以確保這些對象不是NULL,這樣您就可以構建更健壯的方法。

相關問題