Execption Error in c#

Posted by Kumu on Stack Overflow See other posts from Stack Overflow or by Kumu
Published on 2010-04-25T11:18:21Z Indexed on 2010/04/25 11:23 UTC
Read the original article Hit count: 329

Filed under:
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);
        }

       }
    }

This is the whole code and I got same error like previous question.

Unhandled Execption has occuredin you application.If you click...............click Quit.the application will close immediately. Object reference not set to an instance of an object.

And the following details are in the error message.

See the end of this message for details on invoking just-in-time (JIT) debugging instead of this dialog box.

***** Exception Text ******* 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 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

***** Loaded Assemblies ******* mscorlib Assembly Version: 2.0.0.0 Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900) CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v2.0.50727/mscorlib.dll ---------------------------------------- FootballLeague Assembly Version: 1.0.0.0 Win32 Version: 1.0.0.0 CodeBase: file:///C:/Users/achini/Desktop/FootballLeague/FootballLeague/bin/Debug/FootballLeague.exe ---------------------------------------- System.Windows.Forms Assembly Version: 2.0.0.0 Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900) CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll ---------------------------------------- System Assembly Version: 2.0.0.0 Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900) CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll ---------------------------------------- System.Drawing Assembly Version: 2.0.0.0 Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900) CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll ---------------------------------------- System.Configuration Assembly Version: 2.0.0.0 Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900) CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll ---------------------------------------- System.Xml Assembly Version: 2.0.0.0 Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)

CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll

***** JIT Debugging ******* To enable just-in-time (JIT) debugging, the .config file for this application or computer (machine.config) must have the jitDebugging value set in the system.windows.forms section. The application must also be compiled with debugging enabled.

For example:

When JIT debugging is enabled, any unhandled exception will be sent to the JIT debugger registered on the computer rather than be handled by this dialog box.

I need to add the games to using the addGameButton and the save those added games and display them in the list view (gameResultsListView). Now I can added a game and display in the list view.But when I pressed the button addGameButton I got the above error message.

If you can please give me a solution to this problem.

© Stack Overflow or respective owner

Related posts about c#