We help IT Professionals succeed at work.
Get Started

System.NullReferenceException was unhandled by user code

jmicorp
jmicorp asked
on
410 Views
Last Modified: 2012-05-10
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
Senior Software Engineer
CERTIFIED EXPERT
Top Expert 2009
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE