c#中的异常错误

时间:2010-04-25 11:18:22

标签: c# exception

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:Object   引用未设置为的实例   宾语。在   FoolballLeague.MainMenu.addGameButton_Click(对象   发件人,EventArgs e)in   C:\用户\ achini \桌面\ FootballLeague \ FootballLeague \ MainMenu.cs:行   91点   System.Windows.Forms.Control.OnClick(EventArgs的   吃   System.Windows.Forms.Button.OnMouseUp(MouseEventArgs   mevent)at   System.Windows.Forms.Control.WmMouseUp(消息&安培;   m,MouseButtons按钮,Int32点击)   在   System.Windows.Forms.Control.WndProc(消息&安培;   m)at   System.Windows.Forms.ButtonBase.WndProc(消息&安培;   m)at   System.Windows.Forms.Button.WndProc(消息&安培;   m)at   System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息&安培;   m)at   System.Windows.Forms.NativeWindow.Callback(IntPtr的   hWnd,Int32 msg,IntPtr wparam,IntPtr   LPARAM)

我需要添加游戏以使用addGameButton并保存这些添加的游戏并将其显示在列表视图中(gameResultsListView)。 现在我可以添加一个游戏并在列表视图中显示。但是当我按下addGameButton按钮时,我收到了上面的错误信息。

如果可以,请为我解决这个问题。

5 个答案:

答案 0 :(得分:6)

从异常消息中我可以看到你在第91行的addGameButton_Click中有一个NullReferenceException。这是第91行:

footballLeagueDatabase.AddGame(game);

所以footballLeagueDatabase为null。让我们看看你分配给它的代码:

public MainMenu()
{
    InitializeComponent();
    changePanel(1);
}

public MainMenu(FootballLeagueDatabase footballLeagueDatabaseIn)
{
    InitializeComponent();
    footballLeagueDatabase = footballLeagueDatabaseIn;
}

我猜你要么调用错误的构造函数,要么将null对象传递给构造函数。

  

这是整个代码

不,这不是整个代码。您的项目中应该有其他一些文件。错误很可能出现在其中一个文件中(构造此表单的文件)。

答案 1 :(得分:4)

您需要学习读取错误消息和堆栈跟踪。

看看这一点:

  

System.NullReferenceException: Object reference not set to an instance of an object. at FoolballLeague.MainMenu.addGameButton_Click(Object sender, EventArgs e) in C:\Users\achini\Desktop\FootballLeague\FootballLeague\MainMenu.cs:line 91

告诉你错误在哪一行。它还告诉你它是NullReferenceException,这意味着不应该是null

设置一个断点,并逐步执行相关代码,检查变量会发生什么,并弄清楚它是如何以空值结束的。

答案 2 :(得分:3)

您尝试访问的其中一个引用为null。它位于MainMenu.cs文件的第91行。设置断点并查看调试器,什么是null?

我猜想footballLeagueDatabase为null,你需要为它分配一个类型为FootballLeagueDatabase的实例。

答案 3 :(得分:2)

你确定footballLeagueDatabase正在初始化吗?我认为初始化它的构造函数永远不会被调用。

答案 4 :(得分:2)

看起来你似乎没有初始化footballLeagueDatabase任何地方