【问题标题】:Exception Error in c#c#中的异常错误
【发布时间】: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 表单的代码。

标签: c# exception


【解决方案1】:

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

footballLeagueDatabase.AddGame(game);

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

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

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

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

这是完整的代码

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

【讨论】:

    【解决方案2】:

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

    看看这个:

    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

    设置断点,单步执行相关代码,检查变量发生了什么,并找出它是如何以空值结束的。

    【讨论】:

      【解决方案3】:

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

      我猜 footballLeagueDatabase 为空,你需要为其分配一个 FootballLeagueDatabase 类型的实例。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

          您似乎没有在任何地方初始化footballLeagueDatabase

          【讨论】:

            猜你喜欢
            • 2023-03-12
            • 2012-01-02
            • 2014-01-08
            • 1970-01-01
            • 1970-01-01
            • 2017-10-11
            • 1970-01-01
            • 2012-02-14
            • 2015-06-02
            相关资源
            最近更新 更多