'System.NullReferenceException'

Dez

New member
Joined
Apr 25, 2021
Messages
1
Programming Experience
Beginner
can you please evaluate my code and let me know whats wrong please

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;

namespace WindowsFormsApplication2
{
    static class Program
    {

        static TextBox txtBox_1;
        static TextBox txtBox_2;
        static void Main()
        {
            Form Average_Age = new Form();

            // Form appearance

            Average_Age.Width = 250;
            Average_Age.Height = 250;
            Average_Age.BackColor = System.Drawing.Color.White;
            Average_Age.Text = "Demo Form";
            Average_Age.WindowState = FormWindowState.Normal;
            Average_Age.StartPosition = FormStartPosition.CenterScreen;
            Average_Age.MaximizeBox = Average_Age.MinimizeBox = false;

            // Label 1
            Label lblName_1 = new Label();
            lblName_1.Text = " Enter the age of user 1";
            lblName_1.Left = lblName_1.Top = 8;
            lblName_1.AutoSize = true;
            Average_Age.Controls.Add(lblName_1);

            //text box 1
            TextBox txtBox_1 = new TextBox();
            txtBox_1.Left = 8;
            txtBox_1.Top = txtBox_1.Top + txtBox_1.Height + 20;
            txtBox_1.Width = 170;
            Average_Age.Controls.Add(txtBox_1);

            // Label 2
            Label lblName_2 = new Label();
            lblName_2.Text = " Enter the age of user 2";
            lblName_2.Left = lblName_2.Top = 70;
            //lblName_2.Location = newPoint(20, 150);
            lblName_2.AutoSize = true;
            Average_Age.Controls.Add(lblName_2);

            //text box 2
            TextBox txtBox_2 = new TextBox();
            txtBox_2.Left = 8;
            txtBox_2.Top = txtBox_2.Bottom + 30 + txtBox_2.Height + 20;
            txtBox_2.Width = 170;
            Average_Age.Controls.Add(txtBox_2);


            //button 1
            Button bttn_Average = new Button();
            bttn_Average.Text = "Calculate average";
            bttn_Average.Left = 8;
            bttn_Average.Top = txtBox_2.Top + txtBox_2.Height + 20;
            bttn_Average.Click += bttn_AverageClick;
            Average_Age.Controls.Add(bttn_Average);

            //button 2
            Button bttn_Close = new Button();
            bttn_Close.Text = "Close";
            bttn_Close.Left =100;
            bttn_Close.Top = txtBox_2.Top + txtBox_2.Height + 20;
            bttn_Close.Click += bttn_CloseClick;
            Average_Age.Controls.Add(bttn_Close);

            //run
            Application.Run(Average_Age);
        }

        static void bttn_AverageClick(object sender, System.EventArgs e)
        {
        int iAge1 = int.Parse(txtBox_1.Text);
        int iAge2 = int.Parse(txtBox_2.Text);
        int iSum = iAge1 + iAge2;
        double dAverageAge = iSum / 2.0;
        MessageBox.Show ("The average age is " + dAverageAge.ToString());
        }
        static void bttn_CloseClick(object sender, System.EventArgs e)
        {
            Application.Exit();
        }    
    }
}
 
Last edited by a moderator:
The major mistake is that that your static member variables on line 13-14 are shadowed by in scope local variables on lines 37 and 52.

The minor mistake is the use of static class variables instead of instance variables.
 
Back
Top Bottom