Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using an array to sequence a series of images.

Status
Not open for further replies.

mxo

Programmer
May 20, 2005
51
ZA
Hi All

Please help me with my code I keep getting an error as follow: Object reference not set to an instance of an object.

Here is whole code:

<SCRIPT Runat="Server">
Sub Page_Load

If Not Page.IsPostBack Then

'-- Load an array with image urls and save to View State
Dim ImageArray(3) As String
ImageArray(0) = "aspnet2.gif"
ImageArray(1) = "xhtml.gif"
ImageArray(2) = "jsdhtml.gif"
ViewState("PictureArray") = ImageArray

'-- Establish a counter to keep track of the array index
ViewState("Counter") = 0
MyImage.ImageURL = ImageArray(ViewState("Counter"))
Index.Text = ViewState("Counter")

End If

End Sub

Sub Get_Next_Image (Src As Object, Args As EventArgs)

'-- Retrieve the array from view state
Dim ImageArray() As String
ImageArray = ViewState("PictureArray")

'-- Increment the image counter
ViewState("Counter") += 1
If ViewState("Counter") > 2 Then
ViewState("Counter") = 0
End If

'-- Assign the next image to the control
MyImage.ImageURL = ImageArray(ViewState("Counter"))
Index.Text = ViewState("Counter")

End Sub

</SCRIPT>

<form Runat="Server">

<asp:Image id="MyImage" ImageAlign="AbsMiddle" Runat="Server"/>
<asp:Button Text="Next Image" OnClick="Get_Next_Image" Runat="Server"/>
<p>Array index: <asp:Label id="Index" Runat="Server"/></p>

</form>
 
What line does the error occur on?
Also
Code:
ImageArray = ViewState("PictureArray")
You should cast the viewstate object to an array.
 
Also, don't use inline code as you are doing. This is classic ASP style coding. Use the code-behind page instead.
 
Hi Jbenson001

the error occurs on the following line:
MyImage.ImageURL = ImageArray(ViewState("Counter"))

I do not know if iam aswering your question
 
I am not able to replicate the error you are getting. You will have to step through your code while debugging.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top