2011-01-26 60 views
0

有人可以幫我糾正這段代碼嗎?我收到以下錯誤:CType NullReferenceException當試圖上傳圖片

Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. 

Source Error: 

Line 21: 'Determine the maximum pictureID for this user 
Line 22: Dim results As DataView = CType(maxPictureIDDataSource.Select(DataSourceSelectArguments.Empty), DataView) 
Line 23: Dim pictureIDJustAdded As Integer = CType(results(0)(0), Integer) 
Line 24: 'Reference the FileUpload control 
Line 25: Dim imageUpload As FileUpload = CType(dvPictureInsert.FindControl("imageUpload"), FileUpload) 

Source File: C:\Users\Collins\Documents\Visual Studio 2005\WebSites\living to please god world\PhotoAdmin\Default.aspx.vb Line: 23 

和紅色的錯誤線在這行代碼:

Line 23: Dim pictureIDJustAdded As Integer = CType(results(0)(0), Integer) 

沒有人有在那裏我可以開始尋找一個想法?

回答

3

元素結果(0)可能不存在或元素結果(0)(0)可能不存在(取決於前面的語句返回),因此返回Nothing。

所以,你應該先使用CTYPE在他們身上,例如之前檢查這些東西:

Dim pictureIDJustAdded As Integer 
If results(0) IsNot Nothing AndAlso results(0).Length > 0 Then 
    pictureIDJustAdded = CType(results(0)(0), Integer) 
Else 
    'report error 
End If 
+0

您好感謝您的幫助。你能告訴我要插入你的上面的代碼嗎?以下是 – onfire4JesusCollins 2011-01-26 17:30:52