0

これは宿題で、テキスト ボックスにクエスチョン マークの代わりに乱数を入力する数学ゲーム/クイズです。これは、C# を使用した Visual Studio 2012 の 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

これはあなたのコードのすべてですか?参照とは何(Dictionary<string, int>)Application["Ranking"];ですか?そのデータはどこで初期化されますか?

おそらくどこかで初期化する必要があります。そうしないと、null を返しRanking.Add()、null オブジェクトに追加しようとします。

于 2013-10-22T03:20:13.327 に答える