Object reference not set to an instance of an object.

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 13
Reputation: firebirds98 is an unknown quantity at this point 
Solved Threads: 0
firebirds98 firebirds98 is offline Offline
Newbie Poster

Object reference not set to an instance of an object.

 
0
  #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
  1. 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.


  1. Protected Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
  2.  
  3. Dim cart As New Cart
  4. cart = Session("Cart")
  5.  
  6.  
  7. Dim flag As Integer = 1
  8. Dim finishColor As String = ""
  9. Dim knobColor As String = ""
  10. Dim prodQty As Integer = 0
  11. Dim price As Decimal = 0
  12. Dim name As String = ""
  13. Dim productnumber As Integer = 0
  14.  
  15. If txtQty.Text <> "" And IsNumeric(txtQty.Text) Then
  16. prodQty = txtQty.Text
  17. flag = 0
  18. Else
  19. lblError.Text = "Please enter a numeric quantity."
  20. Return
  21. End If
  22.  
  23. productnumber = prodID
  24. name = lblName.Text
  25. price = lblPrice.Text
  26. prodQty = txtQty.Text
  27. finishColor = DDFinish.SelectedItem.Text
  28. knobColor = DDKnob.SelectedItem.Text
  29.  
  30.  
  31.  
  32. cart.AddProduct(productnumber, name, price, prodQty, knobColor, finishColor)
  33.  
  34.  
  35. Session("Cart") = cart
Any help is appreciated!
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 291
Reputation: ericstenson is an unknown quantity at this point 
Solved Threads: 29
Team Colleague
ericstenson's Avatar
ericstenson ericstenson is offline Offline
Posting Whiz in Training

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

 
0
  #2
Apr 11th, 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 Quick reply to this message  
Join Date: Nov 2006
Posts: 32
Reputation: sierrasoft is an unknown quantity at this point 
Solved Threads: 0
sierrasoft sierrasoft is offline Offline
Light Poster

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

 
0
  #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 Quick reply to this message  
Join Date: Aug 2005
Posts: 206
Reputation: plazmo is an unknown quantity at this point 
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.

 
0
  #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 Quick reply to this message  
Join Date: Apr 2008
Posts: 3
Reputation: harivb is an unknown quantity at this point 
Solved Threads: 0
harivb harivb is offline Offline
Newbie Poster

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

 
0
  #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
  1. 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.


  1. Protected Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
  2.  
  3. Dim cart As New Cart
  4. cart = Session("Cart")
  5.  
  6.  
  7. Dim flag As Integer = 1
  8. Dim finishColor As String = ""
  9. Dim knobColor As String = ""
  10. Dim prodQty As Integer = 0
  11. Dim price As Decimal = 0
  12. Dim name As String = ""
  13. Dim productnumber As Integer = 0
  14.  
  15. If txtQty.Text <> "" And IsNumeric(txtQty.Text) Then
  16. prodQty = txtQty.Text
  17. flag = 0
  18. Else
  19. lblError.Text = "Please enter a numeric quantity."
  20. Return
  21. End If
  22.  
  23. productnumber = prodID
  24. name = lblName.Text
  25. price = lblPrice.Text
  26. prodQty = txtQty.Text
  27. finishColor = DDFinish.SelectedItem.Text
  28. knobColor = DDKnob.SelectedItem.Text
  29.  
  30.  
  31.  
  32. cart.AddProduct(productnumber, name, price, prodQty, knobColor, finishColor)
  33.  
  34.  
  35. Session("Cart") = cart
Any help is appreciated!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC