2013-10-22 13 views
0

這是一個家庭作業,一個數學遊戲/測驗,用隨機數代替問號填充文本框。它是使用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()); 
      } 

     } 

    } 
} 
+4

設置一個斷點並逐步執行代碼。我懷疑這個問題實際上是在行之前,而'Ranking'將是'null'。 –

+0

'排名'或'NameText' –

+1

這裏不需要HTML和屏幕截圖。 –

回答

0

您直接使用Application["Ranking"]。在使用之前請檢查它是否爲null。

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

謝謝。當我跑了它,填寫答案和名稱,然後點擊按鈕,我得到一個空白頁。我會一步一步看看它,看看有什麼不對。 – Grafica

0

這是所有的代碼?什麼是(Dictionary<string, int>)Application["Ranking"];引用?數據在哪裏初始化?

您可能需要將它初始化到某個地方,否則它將返回null並且Ranking.Add()將嘗試添加到空對象。

+0

語法是什麼來初始化它? – Grafica

相關問題