2011-01-26 6 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

+1

아래의 사진 업로드에 대한 전체 코드가 있습니다. 22 번째 줄 ('Dim pictureIDJustAdded As Integer'가 코드 조각에 포함되었습니다.)에서 23 번째 줄을 바꾸십시오. – Peladao

관련 문제