c#中的异常错误

Leo*_*Leo 0 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);
        }

       }
    }
Run Code Online (Sandbox Code Playgroud)

这是整个代码,我得到了像上一个问题一样的错误.

您的应用程序中发生了未处理的异常.如果您单击...............单击退出.应用程序将立即关闭.你调用的对象是空的.

以下详细信息在错误消息中.

**************异常文本**************System.NullReferenceException:未将对象引用设置为对象的实例.at FoolballLeague.MainMenu.addGameButton_Click(Object sender,EventArgs e)位于C:\ Users\achini\Desktop\FootballLeague\FootballLeague\MainMenu.cs:System 91的System.Windows.Forms.Control.OnClick(EventArgs e)第91行. System.Windows上System.Windows.Forms.Control.WndProc(Message&m)的System.Windows.Forms.Control.WmMouseUp(Message&m,MouseButtons按钮,Int32单击)中的Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent). System.Windows.Fornd.WandProc上的System.Windows.Forms.Button.WndProc(Message&m)中的Forms.ButtonBase.WndProc(Message&m)在System.Windows.Fornd.NativeWindow.Callback的System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)中IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

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

如果可以,请给我一个解决这个问题的方法.

Mar*_*ers 6

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

footballLeagueDatabase.AddGame(game);
Run Code Online (Sandbox Code Playgroud)

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

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

public MainMenu(FootballLeagueDatabase footballLeagueDatabaseIn)
{
    InitializeComponent();
    footballLeagueDatabase = footballLeagueDatabaseIn;
}
Run Code Online (Sandbox Code Playgroud)

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

这是整个代码

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