943,875 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 6916
  • ASP.NET RSS
Apr 10th, 2008
0

Object reference not set to an instance of an object.

Expand Post »
Im getting a NullReferenceException when I goto add a product to my cart, i get the error when i come to this part
ASP.NET Syntax (Toggle Plain Text)
  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.


ASP.NET Syntax (Toggle Plain Text)
  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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
firebirds98 is offline Offline
15 posts
since Feb 2008
Apr 11th, 2008
0

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

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
Team Colleague
Reputation Points: 14
Solved Threads: 29
Posting Whiz in Training
ericstenson is offline Offline
293 posts
since Dec 2007
Apr 11th, 2008
0

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

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
Reputation Points: 10
Solved Threads: 0
Light Poster
sierrasoft is offline Offline
31 posts
since Nov 2006
Apr 11th, 2008
0

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

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
Reputation Points: 23
Solved Threads: 16
Posting Whiz in Training
plazmo is offline Offline
206 posts
since Aug 2005
Apr 11th, 2008
0

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

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
ASP.NET Syntax (Toggle Plain Text)
  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.


ASP.NET Syntax (Toggle Plain Text)
  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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
harivb is offline Offline
3 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Event Handler
Next Thread in ASP.NET Forum Timeline: database access problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC