0

Can someone help me correct this code? I am receiving the following error:

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 

and the red error line is on this line of code:

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

Does anyone have an idea of where I can start to look?

1 Answer 1

3

The element results(0) may not exist or element results(0)(0) may not exist (depending on what the previous statement returns) and therefore return Nothing.

So you should first check these things before using CType on them, for instance:

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 
Sign up to request clarification or add additional context in comments.

2 Comments

Hello thanks for the help. Can you pls show me were to insert your above code? Here is the full codes for the picture upload below
After line 22 (Dim pictureIDJustAdded As Integer is now included in the code fragment), and replacing your line 23.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.