为什么我会收到错误“对象引用未设置为对象的实例”?

时间:2013-10-22 03:10:21

标签: c# asp.net visual-studio-2012 webforms

这是家庭作业,一个数学游戏/测验,用随机数而不是问号填充文本框。它是使用C#的Visual Studio 2012中的ASP.NET Web窗体。要求是使用某种类型的存储,所以我使用"应用程序"存储用户的姓名和分数,以及点击"检查结果," “结果”面板将显示其名称和分数。错误发生在以下代码的这一行:Ranking.Add(NameText.Text, Score);,"对象引用未设置为对象的实例。"在此先感谢!!!

Layout of Math Game

HTML:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MathGame.aspx.cs" Inherits="MvcApplication3.MathGame" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        #MathGame {
            height: 404px;
            width: 505px;
        }
    </style>
</head>
<body>

        <asp:Panel ID="PanelGame" runat="server">
        <form id="MathGame" runat="server">
        <div>
        <asp:Label runat="server" BorderStyle="None" Font-Names="Microsoft Sans Serif" Font-Size="18pt" style="z-index: 1; top: 142px; position: absolute; height: 33px; text-align: center; right: 762px; margin-right: 87px; bottom: 312px; width: 2px; left: 145px;" Text="-"></asp:Label>
        <asp:Label ID="plusLeftLabel" runat="server" BorderStyle="Solid" Font-Names="Microsoft Sans Serif" Font-Size="18pt" style="z-index: 1; left: 48px; top: 75px; position: absolute; height: 33px; text-align: center; margin-right: 87px" Text="?" Width="60px"></asp:Label>
        <asp:Label ID="plusRightLabel" runat="server" BorderStyle="Solid" Font-Names="Microsoft Sans Serif" Font-Size="18pt" style="z-index: 1; left: 193px; top: 75px; position: absolute; height: 33px; text-align: center; margin-right: 87px" Text="?" Width="60px"></asp:Label>
        <asp:Label runat="server" BorderStyle="None" Font-Names="Microsoft Sans Serif" Font-Size="18pt" style="z-index: 1; left: 270px; top: 208px; position: absolute; height: 33px; text-align: center; margin-right: 87px" Text="=" Width="60px"></asp:Label>
        <asp:Button ID="startButton" runat="server" Font-Size="14pt" style="z-index: 1; left: 265px; top: 379px; position: absolute; height: 34px;" TabIndex="1" Text="Check Results" OnClick="startButton_Click1" ValidationGroup="FormValidation" />

        <asp:Label ID="dividedLeftLabel" runat="server" BorderStyle="Solid" Font-Names="Microsoft Sans Serif" Font-Size="18pt" style="z-index: 1; left: 47px; top: 262px; position: absolute; height: 33px; text-align: center; margin-right: 87px; right: 717px;" Text="?" Width="60px"></asp:Label>
        <asp:Label ID="minusLeftLabel" runat="server" BorderStyle="Solid" Font-Names="Microsoft Sans Serif" Font-Size="18pt" style="z-index: 1; left: 48px; top: 139px; position: absolute; height: 33px; text-align: center; margin-right: 87px" Text="?" Width="60px"></asp:Label>
        <asp:Label ID="timesLeftLabel" runat="server" BorderStyle="Solid" Font-Names="Microsoft Sans Serif" Font-Size="18pt" style="z-index: 1; left: 47px; top: 201px; position: absolute; height: 33px; text-align: center; margin-right: 87px; bottom: 315px;" Text="?" Width="60px"></asp:Label>
        <asp:Label runat="server" BorderStyle="None" Font-Names="Microsoft Sans Serif" Font-Size="18pt" style="z-index: 1; top: 83px; position: absolute; height: 33px; text-align: center; right: 667px; margin-right: 87px; left: 145px; bottom: 426px;" Text="+" Width="20px"></asp:Label>
        <asp:Label runat="server" BorderStyle="None" Font-Names="Microsoft Sans Serif" Font-Size="18pt" style="z-index: 1; top: 269px; position: absolute; height: 33px; text-align: center; right: 667px; margin-right: 87px; left: 145px;" Text="÷" Width="20px"></asp:Label>
        <asp:Label runat="server" BorderStyle="None" Font-Names="Microsoft Sans Serif" Font-Size="18pt" style="z-index: 1; top: 205px; position: absolute; height: 33px; text-align: center; right: 750px; margin-right: 87px; left: 145px;" Text="x" Width="20px"></asp:Label>
        <asp:Label ID="dividedRightLabel" runat="server" BorderStyle="Solid" Font-Names="Microsoft Sans Serif" Font-Size="18pt" style="z-index: 1; left: 193px; top: 262px; position: absolute; height: 33px; text-align: center; margin-right: 87px" Text="?" Width="60px"></asp:Label>
        <asp:Label ID="minusRightLabel" runat="server" BorderStyle="Solid" Font-Names="Microsoft Sans Serif" Font-Size="18pt" style="z-index: 1; left: 193px; top: 138px; position: absolute; height: 33px; text-align: center; margin-right: 87px" Text="?" Width="60px"></asp:Label>
        <asp:Label ID="timesRightLabel" runat="server" BorderStyle="Solid" Font-Names="Microsoft Sans Serif" Font-Size="18pt" style="z-index: 1; left: 192px; top: 202px; position: absolute; height: 33px; text-align: center; margin-right: 87px" Text="?" Width="60px"></asp:Label>
        <asp:Label runat="server" BorderStyle="None" Font-Names="Microsoft Sans Serif" Font-Size="18pt" style="z-index: 1; left: 269px; top: 82px; position: absolute; height: 33px; text-align: center; margin-right: 87px; width: 60px;" Text="="></asp:Label>
        <asp:Label runat="server" BorderStyle="None" Font-Names="Microsoft Sans Serif" Font-Size="18pt" style="z-index: 1; left: 270px; top: 269px; position: absolute; height: 33px; text-align: center; margin-right: 87px" Text="=" Width="60px"></asp:Label>

        <asp:Panel ID="Panel1" runat="server" style="z-index: 1; left: 49px; top: 333px; position: absolute; height: 19px; width: 367px">
        <br />Please enter your name: <asp:TextBox ID="NameText" runat="server" Height="17px"></asp:TextBox><asp:RequiredFieldValidator EnableClientScript="false" ID="RequiredFieldValidator1" runat="server" ErrorMessage="<br />Your name is required." ForeColor="Red" ControlToValidate="NameText" ValidationGroup="FormValidation"></asp:RequiredFieldValidator></asp:Panel>

        <asp:Label runat="server" BorderStyle="None" Font-Names="Microsoft Sans Serif" Font-Size="18pt" style="z-index: 1; left: 269px; top: 147px; position: absolute; height: 33px; text-align: center; margin-right: 87px" Text="=" Width="60px" ID="Label1"></asp:Label>
        <asp:TextBox ID="TimesResult" runat="server" style="z-index: 1; left: 324px; top: 210px; position: absolute; width: 87px; height: 19px"></asp:TextBox>
        <asp:TextBox ID="MinusResult" runat="server" style="z-index: 1; left: 324px; top: 146px; position: absolute; width: 87px; height: 19px"></asp:TextBox>
        <asp:TextBox ID="DivideResult" runat="server" style="z-index: 1; left: 324px; top: 269px; position: absolute; width: 87px; height: 19px"></asp:TextBox>
        <asp:TextBox ID="PlusResult" runat="server" style="z-index: 1; left: 324px; top: 81px; position: absolute; width: 87px; height: 19px"></asp:TextBox>
    </div>
    </form></asp:Panel>

    <asp:Panel ID="PanelRanking" runat="server">
        <asp:BulletedList ID="RankingList" runat="server">
        </asp:BulletedList>
    </asp:Panel>
</body>
</html>

代码背后:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MvcApplication3
{
    public partial class MathGame : System.Web.UI.Page
    {   
        // Create a Random object to generate random numbers.
        Random randomizer = new Random();

        public void Page_Load(object sender, EventArgs e)
        {
            PanelRanking.Visible = false;
            plusLeftLabel.Text = randomizer.Next(51).ToString();
            plusRightLabel.Text = randomizer.Next(51).ToString();
            minusLeftLabel.Text = randomizer.Next(51).ToString();
            minusRightLabel.Text = randomizer.Next(51).ToString();
            timesLeftLabel.Text = randomizer.Next(51).ToString();
            timesRightLabel.Text = randomizer.Next(51).ToString();
            dividedLeftLabel.Text = randomizer.Next(51).ToString();
            dividedRightLabel.Text = randomizer.Next(51).ToString();
        }

        public void startButton_Click1(object sender, EventArgs e)
        {
            int Score = 0;
            // Check the answers

            if (int.Parse(plusLeftLabel.Text) + int.Parse(plusRightLabel.Text) == int.Parse(PlusResult.Text))
                Score += 1;

            if (int.Parse(minusLeftLabel.Text) - int.Parse(minusRightLabel.Text) == int.Parse(MinusResult.Text))
                Score += 1;

            if (int.Parse(timesLeftLabel.Text) + int.Parse(timesRightLabel.Text) == int.Parse(TimesResult.Text))
                Score += 1;

            if (int.Parse(dividedLeftLabel.Text) + int.Parse(dividedRightLabel.Text) == int.Parse(DivideResult.Text))
                Score += 1;

            Dictionary<string, int> Ranking = (Dictionary<string, int>)Application["Ranking"];
            *Ranking.Add(NameText.Text, Score);*
            Application.Lock();
            Application["Ranking"] = Ranking;
            Application.UnLock();

            foreach (KeyValuePair<string, int> element in Ranking){
                RankingList.Items.Add(element.ToString());
            }

        }

    }
}

2 个答案:

答案 0 :(得分:0)

这是你的全部代码吗?什么是(Dictionary<string, int>)Application["Ranking"];引用?数据初始化在哪里?

您可能需要在某处初始化它,否则它将返回null并且Ranking.Add()将尝试添加到空对象。

答案 1 :(得分:0)

您直接使用Application["Ranking"]。请在使用之前检查它是否为空。

if(Application["Ranking"] != null){
}