【发布时间】:2010-04-25 11:18:21
【问题描述】:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace FoolballLeague
{
public partial class MainMenu : Form
{
FootballLeagueDatabase footballLeagueDatabase;
Game game;
Login login;
public MainMenu()
{
InitializeComponent();
changePanel(1);
}
public MainMenu(FootballLeagueDatabase footballLeagueDatabaseIn)
{
InitializeComponent();
footballLeagueDatabase = footballLeagueDatabaseIn;
}
private void Form_Loaded(object sender, EventArgs e)
{
}
private void gameButton_Click(object sender, EventArgs e)
{
int option = 0;
changePanel(option);
}
private void scoreboardButton_Click(object sender, EventArgs e)
{
int option = 1;
changePanel(option);
}
private void changePanel(int optionIn)
{
gamePanel.Hide();
scoreboardPanel.Hide();
string title = "Football League System";
switch (optionIn)
{
case 0:
gamePanel.Show();
this.Text = title + " - Game Menu";
break;
case 1:
scoreboardPanel.Show();
this.Text = title + " - Display Menu";
break;
}
}
private void logoutButton_Click(object sender, EventArgs e)
{
login = new Login();
login.Show();
this.Hide();
}
private void addGameButton_Click(object sender, EventArgs e)
{
if ((homeTeamTxt.Text.Length) == 0)
MessageBox.Show("You must enter a Home Team");
else if (homeScoreUpDown.Value > 9 || homeScoreUpDown.Minimum < 0)
MessageBox.Show("You must enter one digit between 0 and 9");
else if ((awayTeamTxt.Text.Length) == 0)
MessageBox.Show("You must enter a Away Team");
else if (homeScoreUpDown.Value > 9 || homeScoreUpDown.Value < 0)
MessageBox.Show("You must enter one digit between 0 to 9");
else
{
//checkGameInputFields();
game = new Game(homeTeamTxt.Text, int.Parse(homeScoreUpDown.Value.ToString()), awayTeamTxt.Text, int.Parse(awayScoreUpDown.Value.ToString()));
MessageBox.Show("Home Team -" + '\t' + homeTeamTxt.Text + '\t' + "and" + '\r' + "Away Team -" + '\t' + awayTeamTxt.Text + '\t' + "created");
footballLeagueDatabase.AddGame(game);
//clearCreateStudentInputFields();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
displayDateAndTime();
}
private void displayDateAndTime()
{
dateLabel.Text = DateTime.Today.ToLongDateString();
timeLabel.Text = DateTime.Now.ToShortTimeString();
}
private void displayResultsButton_Click(object sender, EventArgs e)
{
Game game = new Game(homeTeamTxt.Text, int.Parse(homeScoreUpDown.Value.ToString()), awayTeamTxt.Text, int.Parse(awayScoreUpDown.Value.ToString()));
gameResultsListView.Items.Clear();
gameResultsListView.View = View.Details;
ListViewItem row = new ListViewItem();
row.SubItems.Add(game.HomeTeam.ToString());
row.SubItems.Add(game.HomeScore.ToString());
row.SubItems.Add(game.AwayTeam.ToString());
row.SubItems.Add(game.AwayScore.ToString());
gameResultsListView.Items.Add(row);
}
private void displayGamesButton_Click(object sender, EventArgs e)
{
Game game = new Game("Home", 2, "Away", 4);//homeTeamTxt.Text, int.Parse(homeScoreUpDown.Value.ToString()), awayTeamTxt.Text, int.Parse(awayScoreUpDown.Value.ToString()));
modifyGamesListView.Items.Clear();
modifyGamesListView.View = View.Details;
ListViewItem row = new ListViewItem();
row.SubItems.Add(game.HomeTeam.ToString());
row.SubItems.Add(game.HomeScore.ToString());
row.SubItems.Add(game.AwayTeam.ToString());
row.SubItems.Add(game.AwayScore.ToString());
modifyGamesListView.Items.Add(row);
}
}
}
这是整个代码,我得到了和上一个问题一样的错误。
您发生了未处理的异常 应用程序。如果你 点击......点击退出。 应用程序将立即关闭。 对象引用未设置为 对象的实例。
错误信息中包含以下详细信息。
**************异常文本************** System.NullReferenceException:对象 引用未设置为 目的。在 FoolballLeague.MainMenu.addGameButton_Click(对象 发件人,EventArgs e) 在 C:\Users\achini\Desktop\FootballLeague\FootballLeague\MainMenu.cs:line 91 在 System.Windows.Forms.Control.OnClick(EventArgs 吃 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件)在 System.Windows.Forms.Control.WmMouseUp(消息& m,MouseButtons 按钮,Int32 点击) 在 System.Windows.Forms.Control.WndProc(消息& m) 在 System.Windows.Forms.ButtonBase.WndProc(消息& m) 在 System.Windows.Forms.Button.WndProc(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息& m) 在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr 参数)
我需要使用 addGameButton 添加游戏并保存这些添加的游戏并将它们显示在列表视图 (gameResultsListView) 中。 现在我可以添加一个游戏并在列表视图中显示。但是当我按下按钮 addGameButton 时,我收到了上述错误消息。
如果可以的话,请给我一个解决这个问题的方法。
【问题讨论】:
-
您可能需要稍微缩小范围以获得一个好的答案。不需要异常的全部内容(只需要类型和堆栈跟踪),而且您可能希望删除与您的问题无关的部分代码。
-
@Andy:虽然发布不相关的代码是浪费时间并且让我们难以阅读,但从这个问题来看,我猜发帖者不知道什么是相关的或不相关的邮政。发布太多总比发布太少好。在这种情况下,发布完整文件可以让我准确找出是哪一行引发了错误——发帖者可能自己不知道该怎么做。当然,他们应该自己学习如何做到这一点。
-
这是您项目中唯一的表单吗?是否有代码来初始化并显示此代码,或者您是否将其设置为启动表单?如果这是启动形式,那么很可能没有调用发送 footballLeagueDatabase 的构造函数,而是调用没有参数的构造函数。最简单的测试方法是设置一些断点;否则,您可以执行在每个构造函数中放置一些消息框的肮脏方法,以查看正在调用哪个消息框。如果调用了无参数构造函数,那么您已经找到了问题。否则,请查看创建 MainMenu 表单的代码。