Exception 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:43 UTC
Read the original article Hit count: 274

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 Exception has occurred in 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.

***** 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)

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 add 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#

Related posts about exception