0

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

数学游戏布局

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());
            }

        }

    }
}
4

2 回答 2

0

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

if(Application["Ranking"] != null){
}
于 2013-10-22T04:06:34.713 回答
0

这是你所有的代码吗?什么是(Dictionary<string, int>)Application["Ranking"];参考?该数据在哪里初始化?

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

于 2013-10-22T03:20:13.327 回答