By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,201 Members | 937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,201 IT Pros & Developers. It's quick & easy.

Null Reference Exception when run under machine name

P: n/a
I am having a very stange issue happen to my web application. I am
trying to develop a simple shopping cart. The code works perfectly
when I run the application under http://localhost/myapp. However, when
I try to run the same app under the machine name http://mymachine/myapp
I recieve a Null Reference Exception and the page crashes. I can't
figure out why this is happening. The Null Reference is referring to a
Session variable containing a dynamic DataTable which has the items the
user has chosen to purchase. The Store and the Shopping Cart are all
in 1 web form (only trying to sell t-shirts). Can anyone give me a
hint as to what may be happening. I am attaching the necessary code
snippets. Thanks in advance.

Dim objDT as DataTable = New DataTable
Dim objDR as DataRow
Private Sub Page_Load(ByVal sender as System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then
makeCart()
End If

Function makeCart()
objDT = New System.Data.DataTable("Cart")
'create the columns to store the items in the shopping cart
Session("Cart") = objDT
End Function

Public Sub AddToCart(ByVal s As Object, ByVal e As EventArgs) 'this
handles the click event to add items to the cart.
objDT = Session("Cart")
For Each objDR In objDT.Rows 'this is the line which throws the
Null Reference error. After debugging I found that the Session("Cart")
= Nothing. As I mentioned above, this problem does not occur when run
under 'localhost'.
'do some stuff here
Next
End Sub

Jul 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
the way i did in mastermind and ludo, chinese-checker, draughts, etc,
games using gdi in vb. i know errror problem when creating games. there
is nothing wrong with ur code. the only problem is coming from page_load
procedure..that is only way i know is coming from load event because i
used games programming .
this is how i do in games:

Dim PegHole As Image
Private Function CreatePegHoleImage() As Image
\\
\\
end Function
if i didn't add in form_load...i will get same error.

if i add in form_load( )
private sub form_load(byval........, ..........)
PegHole = Me.CreatePegHoleImage() <===== that is only way prevent error
end sub

but i haven't done yet datattable....but u can try this in below...

Private Sub Page_Load(ByVal sender as System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
objDR=me.makecart()
End If

regards


Shannon wrote:
I am having a very stange issue happen to my web application. I am
trying to develop a simple shopping cart. The code works perfectly
when I run the application under http://localhost/myapp. However, when
I try to run the same app under the machine name http://mymachine/myapp
I recieve a Null Reference Exception and the page crashes. I can't
figure out why this is happening. The Null Reference is referring to a
Session variable containing a dynamic DataTable which has the items the
user has chosen to purchase. The Store and the Shopping Cart are all
in 1 web form (only trying to sell t-shirts). Can anyone give me a
hint as to what may be happening. I am attaching the necessary code
snippets. Thanks in advance.

Dim objDT as DataTable = New DataTable
Dim objDR as DataRow
Private Sub Page_Load(ByVal sender as System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then
makeCart()
End If

Function makeCart()
objDT = New System.Data.DataTable("Cart")
'create the columns to store the items in the shopping cart
Session("Cart") = objDT
End Function

Public Sub AddToCart(ByVal s As Object, ByVal e As EventArgs) 'this
handles the click event to add items to the cart.
objDT = Session("Cart")
For Each objDR In objDT.Rows 'this is the line which throws the
Null Reference error. After debugging I found that the Session("Cart")
= Nothing. As I mentioned above, this problem does not occur when run
under 'localhost'.
'do some stuff here
Next
End Sub


Jul 21 '05 #2

P: n/a

Thanks for the tip, but unfortunately the problem still exists. I will
try to work out some variations of how the variable is loaded based upon
your comments. If you have further insight, please let me know.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.