simple shopping cart system problem

vbdotnetrookie

New member
Joined
Apr 4, 2007
Messages
2
Programming Experience
Beginner
Hi, i have gotten the school assignment to create a simple shopping cart system in vb.net, using arraylists. I started out, but being a vb.net rookie i soon stumbled into a problem, which i haven't been able to solve yet, so i turn to you peeps, more experienced vb.net programmers, for some help in solving this.

The following code makes a list of items the user can pick, they can select it, then click the button to add it to their shopping cart (aka winkelwagen in my code)
all looked well till i tried to add an item to my shopping cart, and got the following error: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

it's a common error in vb.net i read everywhere, but none the less, i can't seem to figure out what's wrong, any help would be greatly appreciated

as for the code, here it is:

VB.NET:
 <%@ Page Language="VB" Debug="true" %>
<script runat="server">
Dim lijst, winkelwagen As Arraylist
sub Page_Load(sender As Object, e As System.EventArgs)
    if not ispostback then
        lijst = new Arraylist
        winkelwagen = new arraylist
        lijst.add("Chips")
        lijst.add("Choco")
        lijst.add("Toiletpapier")
        lijst.add("Vuilniszakken")
        lijst.add("Aardbeien")
        rb.DataSource = lijst
        rb.DataBind()
    end if
    rb2.DataSource = winkelwagen
    rb2.DataBind()
end sub
sub toevoegen(sender As Object, e As System.EventArgs)
    winkelwagen.add(rb.SelectedItem.tostring)
end sub
 
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <asp:RadioButtonList id="rb" runat="server"></asp:RadioButtonList>
        <br />
        <asp:Button id="VoegToe" onclick="toevoegen" runat="server" Text="Voeg toe"></asp:Button>
        <br />
        <asp:RadioButtonList id="rb2" runat="server"></asp:RadioButtonList>
        <br />
    </form>
</body>
</html>
 
Try this instead of your other code-behind:
HTML:
<script runat="server">
    Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not IsPostBack Then
            Dim lijst As ArrayList = New ArrayList
            lijst.Add("Chips")
            lijst.Add("Choco")
            lijst.Add("Toiletpapier")
            lijst.Add("Vuilniszakken")
            lijst.Add("Aardbeien")
            rb.DataSource = lijst
            rb.DataBind()
            Session("cart") = New ArrayList
        End If
    End Sub
 
    Sub toevoegen(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim winkelwagen As ArrayList = Session("cart")
        winkelwagen.Add(rb.SelectedItem.ToString)
        Session("cart") = winkelwagen
        rb2.DataSource = winkelwagen
        rb2.DataBind()
    End Sub
</script>
Also check this INFO: ASP.NET State Management Overview
 
Back
Top