System.NullReferenceException was unhandled by user code

jmicorp
jmicorp used Ask the Experts™
on
On my home page I have a survey which works fine when there is a survey present for the day. The following is the default.aspx page:


<%@ Page Language="C#" MasterPageFile="~/Global.master" AutoEventWireup="true" Inherits="_default" Title="Daily Prizes at 365 Days of Beauty | NewBeauty Magazine" EnableViewState="false" Codebehind="default.aspx.cs" %>
<%@ MasterType TypeName="Global" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
     <link type='text/css' href='/includes/css/basic.css' rel='stylesheet' media='screen' />
      <!-- IE 6 hacks -->
      <!--[if lt IE 7]>
      <link type='text/css' href='/includes/css/basic_ie.css' rel='stylesheet' media='screen' />
      <![endif]-->
   
    <script src="includes/js/jquery/jquery.simplemodal.js" type="text/javascript"></script>
   
    <script type="text/javascript">
        $(function() {
            var height1 = $('#col-main').height();
            var height2 = $('#col-right').height();
            if (height1 > height2)
            {
                setHeight(height1);
            }
            else
            {
                setHeight(height2);
            }
        });
       
        function setHeight(maxHeight) {
            $('#col-main').height(maxHeight);
            $('#col-right').height(maxHeight);
            $('.content-box').shadow({ color: "#444", offset: 5, opacity: 0.2 });
            $('.content-box').shadow({ color: "#444", offset: 5, opacity: 0.2 });
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Panel ID="pnlContest" runat="server">
<div id="home-wrapper">
    <!-- Main -->
    <div id="col-main" class="content-box blue-bg">
        <img src="/images/headers/enter-daily.gif" class="title" style="margin-left: -2px;" alt="Daily Giveaway" />
            <div style="padding: 0 0 0 9px;">
            <!-- Prize -->
            <div id="prize-wrapper">
                <asp:HyperLink ID="hlTodaysPrize" Target="_blank" runat="server"><asp:Image ID="imgTodaysPrize" CssClass="fl" runat="server" /></asp:HyperLink>
                <p class="fl" style="margin: 30px 15px 0 25px; width: 210px;">
                    <span id="prize-name"><asp:Literal ID="ltrlTodaysPrize" runat="server" /></span><br />
                          ($<asp:Literal ID="ltrlTodayPrizeValue" runat="server" /> value)<br /><br />
                          <asp:Literal ID="ltrlPrizeDescription" runat="server" />
                      </p>
            </div>
            <p style="margin-bottom:15px;">Tell us what you think about beauty, and you could win today's fabulous beauty prize!</p>
            <!-- Survey -->
            <div class="fl" style="width:260px;">
                <sandow:Survey ID="Survey" runat="server" />
            </div>
            <div class="fl" style="width:170px; margin-left:30px;">
                <asp:Label ID="lblRules" runat="server" CssClass="survey-rules">
                            By clicking on Enter To Win, you are indicating that you have read and agree to the <a href="#" id="a-official-rules">Official Rules</a>
                      </asp:Label>
                      <asp:ImageButton ID="btnSubmit" ImageUrl="~/images/btn-enter-to-win.gif" AlternateText="enter me to win" CssClass="image-button" runat="server" onclick="btnSubmit_Click" /><br />
                      <a href="#" id="upcoming-prizes" title="See next week's prizes"><img src="images/see-next-weeks-prizes.gif" alt="See next week's prizes" /></a>
            </div>
        </div>
    </div>
    <!-- Previous Winners -->
    <div id="col-right" class="content-box blue-bg">
        <img src="/images/headers/previous-winners.gif" class="title" alt="previous winners" />
        <p>See all of the lucky <a href="/past-winners.aspx" class="green" style="padding-top:8px;">daily winners here</a>.</p>
        <asp:Literal ID="ltrlPreviousWinners" runat="server" />
    </div>
</div>
</asp:Panel>
<div id="official-rules"></div>
<div id="basicModalContent" style='display:none'>
      <img src="/images/headers/next-weeks-prizes.gif" alt="next week's prizes" class="title" />
    <asp:Literal ID="ltrlUpcomingPrizes" runat="server"></asp:Literal>
</div>
</asp:Content>

If there is no survey present for the day, the page dies. I would like to change the code to where the page will continue if there is no survey present.

The default.aspx.cs where the page dies is this:

        // Get Today's Prize Information
        var prize = Master.Survey.Prize;          <-----------SPECIFICALLY HERE IS WHERE I GET THE ERROR BELOW
        if (!String.IsNullOrEmpty(prize.Url))
        {
            hlTodaysPrize.NavigateUrl = prize.Url;
        }
        else
        {
            hlTodaysPrize.Enabled = false;
        }


I get the following error message:

System.NullReferenceException was unhandled by user code
  Message=Object reference not set to an instance of an object.
  Source=Sandow.365
  StackTrace:
       at _default.Page_Load(Object sender, EventArgs e) in C:\Sandow_Sites\365.newbeauty.com\trunk\Sandow.365\default.aspx.cs:line 61
       at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
       at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at Sandow.CommonBaseClasses.SandowPage.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException:
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Software Engineer
Top Expert 2009
Commented:
Try:
if Master.Survey == null
  return; // or do any defaults for no survey here
 var prize = Master.Survey.Prize;    

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial