Im getting a NullReferenceException when I goto add a product to my cart, i get the error when i come to this part

cart.AddProduct(productnumber, name, price, prodQty, knobColor, finishColor)

I use extremely similar code in another one of my projects and do not come across this problem, I have also researched it for a while and still came across no soltuion.

Protected Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Dim cart As New Cart
        cart = Session("Cart")


        Dim flag As Integer = 1
        Dim finishColor As String = ""
        Dim knobColor As String = ""
        Dim prodQty As Integer = 0
        Dim price As Decimal = 0
        Dim name As String = ""
        Dim productnumber As Integer = 0

        If txtQty.Text <> "" And IsNumeric(txtQty.Text) Then
            prodQty = txtQty.Text
            flag = 0
        Else
            lblError.Text = "Please enter a numeric quantity."
            Return
        End If

        productnumber = prodID
        name = lblName.Text
        price = lblPrice.Text
        prodQty = txtQty.Text
        finishColor = DDFinish.SelectedItem.Text
        knobColor = DDKnob.SelectedItem.Text



        cart.AddProduct(productnumber, name, price, prodQty, knobColor, finishColor)


        Session("Cart") = cart

Any help is appreciated!

Something is returning a returning noting as a value.... try running it on the VS test server and see where it breaks, look at the data within the string... you will see it is "" (null). you may need to change the .text to .text.tostring

This error occurs when you don't use the "New" keyword to instantiate the Object. Your code should be either of the following 2:
Dim cart as New Cart
OR
Dim cart as Cart
cart = New Cart

Regards
Sunil Punjabi

Session("Cart") is null
So either check if Session("Cart") is null, and create a new one if it doesnt exists. or create a new Session("Cart") when the session is started.

Session("Cart") = New Cart

Session("Cart") is not initialized yet. So make sure You need to check Session("Cart") with null(nothing) and assign it properly. You need to do something like this.

Dim cart As New Cart
cart = Session("Cart")
if (cart == Nothing)
cart = new cart();

then use cart object. you won't get this type of error.


Im getting a NullReferenceException when I goto add a product to my cart, i get the error when i come to this part

cart.AddProduct(productnumber, name, price, prodQty, knobColor, finishColor)

I use extremely similar code in another one of my projects and do not come across this problem, I have also researched it for a while and still came across no soltuion.

Protected Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Dim cart As New Cart
        cart = Session("Cart")


        Dim flag As Integer = 1
        Dim finishColor As String = ""
        Dim knobColor As String = ""
        Dim prodQty As Integer = 0
        Dim price As Decimal = 0
        Dim name As String = ""
        Dim productnumber As Integer = 0

        If txtQty.Text <> "" And IsNumeric(txtQty.Text) Then
            prodQty = txtQty.Text
            flag = 0
        Else
            lblError.Text = "Please enter a numeric quantity."
            Return
        End If

        productnumber = prodID
        name = lblName.Text
        price = lblPrice.Text
        prodQty = txtQty.Text
        finishColor = DDFinish.SelectedItem.Text
        knobColor = DDKnob.SelectedItem.Text



        cart.AddProduct(productnumber, name, price, prodQty, knobColor, finishColor)


        Session("Cart") = cart

Any help is appreciated!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.