User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 373,936 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,582 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 617 | Replies: 4
Reply
Join Date: Feb 2008
Posts: 8
Reputation: firebirds98 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
firebirds98 firebirds98 is offline Offline
Newbie Poster

Object reference not set to an instance of an object.

  #1  
Apr 10th, 2008
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!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2007
Posts: 286
Reputation: ericstenson is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 28
Colleague
ericstenson's Avatar
ericstenson ericstenson is offline Offline
Posting Whiz in Training

Re: Object reference not set to an instance of an object.

  #2  
Apr 10th, 2008
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
--
"Dummy."
Reply With Quote  
Join Date: Nov 2006
Location: Nasik-India
Posts: 21
Reputation: sierrasoft is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
sierrasoft sierrasoft is offline Offline
Newbie Poster

Re: Object reference not set to an instance of an object.

  #3  
Apr 11th, 2008
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
Reply With Quote  
Join Date: Aug 2005
Location: Ohio
Posts: 204
Reputation: plazmo is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 16
plazmo's Avatar
plazmo plazmo is offline Offline
Posting Whiz in Training

Re: Object reference not set to an instance of an object.

  #4  
Apr 11th, 2008
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
Reply With Quote  
Join Date: Apr 2008
Posts: 3
Reputation: harivb is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
harivb harivb is offline Offline
Newbie Poster

Re: Object reference not set to an instance of an object.

  #5  
Apr 11th, 2008
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.





Originally Posted by firebirds98 View Post
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!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb ASP.NET Marketplace
Thread Tools Display Modes

Other Threads in the ASP.NET Forum

All times are GMT -4. The time now is 6:36 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC