0

When trying to save an order we first load the object from session. This works. Then we add information to the order and put it back into session. I then receive and Object reference not set to an instance of an object error.

Private Sub SaveOrder()
    'load the order
    Order = Session("Order")

    'Add order information here

    Session("Order") = Order 'The error is occurring here.
End Sub

The stack trace is as follows:

    Error Path: Contact.aspx
    Error Detail: Object reference not set to an instance of an object.
    Error Source: Void __RenderContent1(System.Web.UI.HtmlTextWriter, System.Web.UI.Control)
    Error Stack Trace:    at ASP.orderentry_uc3_contact_aspx.__RenderContent1(HtmlTextWriter __w, Control parameterContainer) in C:\Portal Websites\Portals\OrderEntry\UC3\Contact.aspx:line 9
   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   at System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer)
   at System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer)
   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
   at System.Web.UI.Page.Render(HtmlTextWriter writer)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Any help with this would be greatly appreciate.

Added requested code:

Imports ComponentArt.Web.UI

Partial Class OrderEntry_UC3_Contact
Inherits Company.Pages.CompanyPage
Public Order As Company.Orders.UC3.Order

Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
    If Valid() Then
        SaveOrder() 'This is line 9
        Response.Redirect("Payment.aspx")
    End If
End Sub

I've added the top of the aspx page (The whole thing is too large).

<%@ Page Language="VB" MasterPageFile="~/Portal/PortalNoFooter.master" AutoEventWireup="false"
    EnableTheming="true" CodeFile="Contact.aspx.vb" Inherits="OrderEntry_UC3_Contact"
    Title="Contact" %>

<%@ Register TagPrefix="ComponentArt" Namespace="ComponentArt.Web.UI" Assembly="ComponentArt.Web.UI" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
      //To large to submit the whole thing
</asp:Content>
enter code here
  • Double check your code. The reference variable `Order` might be assigned to `Nothing` (null). – KV Prajapati Aug 16 '12 at 03:29
  • Can you show the code from the top of the aspx file? Be sure to include up to least line 9, which is indicated in the error message. – TLS Aug 16 '12 at 03:35
  • Order is not nothing. Otherwise it would crash while savings things to it (both primitives and complex objects). – Chris Gielis Aug 16 '12 at 03:35
  • The code you have shown us and the stack trace does not add up. Can you show us the aspx page as well? – ChaosPandion Aug 16 '12 at 03:40
  • Thanks for adding the code. Are you using a code-behind file or inline server-side code? The stack trace is pointing to something during the rendering event, which should happen after your button event handler. I'm not sure we have enough info yet to provide proper assistance. What else can you give us? – TLS Aug 16 '12 at 03:41
  • It's a code behind file. The stack trace seems to mostly be pointing to ASP core functionality (System.Web, etc). Unfortunately I can't debug the error as only our clients (and only a few of them) seem to be effected by the error and we can't replicate it. – Chris Gielis Aug 16 '12 at 03:46
  • Please post the top of the aspx file. You have posted the top of the vb file. The stack trace is pointing to something in the aspx file. Is there a server-side tag on line 9 in the aspx file? – TLS Aug 16 '12 at 03:48
  • var orderType = <%= Order.orderType %>; That is line 9 of the .aspx file, it is in a javascript block. The order is loaded from session on page_load after we check whether the session has expired. – Chris Gielis Aug 16 '12 at 03:54
  • I will try a change to see if it is that that's causing it. – Chris Gielis Aug 16 '12 at 03:57
  • Is the `Order` object being set to Nothing at some point in the `Page_Load` or some other method that executes before the page is rendered? And, when you load the value from Session, do you make sure it's not Nothing then or just assume that it's not Nothing? – TLS Aug 16 '12 at 03:57
  • No, but it is being loaded from session during Page_Load if the page is not a postback. If it is nothing we throw an exception. – Chris Gielis Aug 16 '12 at 04:00

1 Answers1

1

Your code examples don't show the Page_Load or the exact lines around line 9 of the aspx file, but it sounds like you are assuming that Order will be populated at all times. If you are only loading the Order object in Page_Load during a non-postback, when the user clicks the button, the Order object is not being loaded early enough in the page lifecycle.

Try removing the check for IsPostback and always reload the Order object from the Session.

TLS
  • 3,090
  • 2
  • 25
  • 33