开发者

The project compiles perfectly, but I get an unhandled exception and have no idea why

I get an unhandled exception that says:

System.NullReferenceException: Object reference not set to an instance of an object.
at Eagle_Eye_Class_Finder.GetSchedule.GetDataFromNumber(String ID) in C:\Users\Joshua Banks\Desktop\EET Project\Eagle Eye Class Finder\GetSchedule.cs:line 24
at Eagle_Eye_Class_Finder.Form1.button2_Click(Object sender, EventArgs e) in C:\Users\Joshua Banks\Desktop\EET Project\Eagle Eye Class Finder\Form1.cs:line 196
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.PerformClick()
at System.Windows.Forms.Form.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.TextBoxBase.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.Control.PreProcessMessage(Message& msg)
at System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg)
at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)

Here is my get schedule class:

using System;
using System.IO;
using System.Data;
using System.Text;
using System.Drawing;
using System.Data.OleDb;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Collections.Generic;

namespace Eagle_Eye_Class_Finder
{
    public class GetSchedule
    {
        public string GetDataFromNumber(string ID)
        {
            IDnumber[] IDnumbers = new IDnumber[3];
            foreach (IDnumber IDCandidateMatch in IDnumbers)
            {
                if (IDCandidateMatch.ID == ID)
                {
                    StringBuilder myData = new StringBuilder();
                    myData.AppendLine(IDCandidateMatch.Name);
                    myData.AppendLine(": ");
                    myData.AppendLine(IDCandidateMatch.ID);
                    myData.AppendLine(IDCandidateMatch.year);
                    myData.AppendLine(IDCandidateMatch.class1);
                    myData.AppendLine(IDCandidateMatch.class2);
                    myData.AppendLine(IDCandidateMatch.class3);
                    myData.AppendLine(IDCandidateMatch.class4);
                    //return myData;
                    return myData.ToString();
                }
            }
            return "";
        }

        public GetSchedule()
        {
            IDnumber[] IDnumbers = new IDnumber[3];
            IDnumbers[0] = new IDnumber() { Name = "Joshua Banks", ID = "900456317", year = "Senior", class1 = "TEET 4090", class2 = "TEET 3020", class3 = "TEET 3090", class4 = "TEET 4290" };
            IDnumbers[1] = new IDnumber() { Name = "Sean Ward", ID = "900456318", year = "Junior", class1 = "ENGNR 4090", class2 = "ENGNR 3020", class3 = "ENGNR 3090", class4 = "ENGNR 4290" };
            IDnumbers[2] = new IDnumber() { Name = "Terrell Johnson", ID = "900456319", year = "Sophomore", class1 = "BUS 4090", class2 = "BUS 3020", class3 = "BUS 3090", class4 = "BUS 4290" };
        }

        public class IDnumber
        {
            public string Name { get; set; }
            public string ID { get; set; }
            public string year { get; set; }
            public string class1 { get; set; }
            public string class2 { get; set; }
            public string class3 { get; set; }
            public string class4 { get; set; }

            public static void ProcessNumber(IDnumber myNum)
            {
                StringBuilder myData = new StringBuilder();
                myData.AppendLine(myNum.Name);
                myData.AppendLine(": ");
                myData.AppendLine(myNum.ID);
                myData.AppendLine(myNum.year);
                myData.AppendLine(myNum.class1);
                myData.AppendLine(myNum.class2);
                myData.AppendLine(myNum.class3);
                myData.AppendLine(myNum.class4);
                MessageBox.Show(myData.ToString());
            }
        }
    }
}

Here is my Form 1 class:

using System;
using System.IO;
using System.Data;
using System.Text;
using System.Drawing;
using System.Data.OleDb;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing.Printing;
using System.Collections.Generic;

namespace Eagle_Eye_Class_Finder
{
    /// This form is the entry form, it is the first form the user will see when the app is run.
    ///

    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.ProgressBar progressBar1;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.DateTimePicker dateTimePicker1;
        private IContainer components;
        private Timer timer1;
        private BindingSource form1BindingSource;
        public static Form Mainform = null;

        // creates new instance of second form
        YOURCLASSSCHEDULE SecondForm = new YOURCLASSSCHEDULE();

        public Form1()
        {
            InitializeComponent();

            // TODO: Add any constructor code after InitializeComponent call

        }

        /// Clean up any resources being used.

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.progressBar1 = new System.Windows.Forms.ProgressBar();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.button2 = new System.Windows.Forms.Button();
            this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.form1BindingSource = new System.Windows.Forms.BindingSource(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.form1BindingSource)).BeginInit();
            this.SuspendLayout();
            //
            // textBox1
            //
            this.textBox1.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.form1BindingSource, "Text", true, System.Windows.Forms.DataSourceUpdateMode.OnValidation, null, "900456317"));
            this.textBox1.Location = new System.Drawing.Point(328, 280);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(208, 20);
            this.textBox1.TabIndex = 2;
            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            //
            // progressBar1
            //
            this.progressBar1.Location = new System.Drawing.Point(258, 410);
            this.progressBar1.MarqueeAnimationSpeed = 10;
            this.progressBar1.Name = "progressBar1";
            this.progressBar1.Size = new System.Drawing.Size(344, 8);
            this.progressBar1.TabIndex = 3;
            this.progressBar1.Click += new System.EventHandler(this.progressBar1_Click);
            //
            // pictureBox1
            //
            this.pictureBox1.BackColor = System.Drawing.SystemColors.ControlLightLight;
            this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
            this.pictureBox1.Location = new System.Drawing.Point(680, 400);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(120, 112);
            this.pictureBox1.TabIndex = 4;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
            //
            // button2
            //
            this.button2.Font = new System.Drawing.Font("Mistral", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button2.Image = ((System.Drawing.Image)(resources.GetObject("button2.Image")));
            this.button2.Location = new System.Drawing.Point(699, 442);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(78, 28);
            this.button2.TabIndex = 5;
            this.button2.Text = "OK";
            this.button2.Click += new System.EventHandler(this.button2_Click);
            //
            // dateTimePicker1
            //
            this.dateTimePicker1.Location = new System.Drawing.Point(336, 104);
            this.dateTimePicker1.Name = "dateTi开发者_运维问答mePicker1";
            this.dateTimePicker1.Size = new System.Drawing.Size(200, 20);
            this.dateTimePicker1.TabIndex = 6;
            this.dateTimePicker1.ValueChanged += new System.EventHandler(this.dateTimePicker1_ValueChanged);
            //
            // timer1
            //
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            //
            // form1BindingSource
            //
            this.form1BindingSource.DataSource = typeof(Eagle_Eye_Class_Finder.Form1);
            //
            // Form1
            //
            this.AcceptButton = this.button2;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.ClientSize = new System.Drawing.Size(856, 556);
            this.Controls.Add(this.dateTimePicker1);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.progressBar1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "Eagle Eye Class Finder";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.form1BindingSource)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
        }
        #endregion

        /// The main entry point for the application.

        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());

        }

        public void Form1_Load(object sender, System.EventArgs e)
        {

        }

        public void textBox1_TextChanged(object sender, System.EventArgs e)
        {
            //allows only numbers to be entered in textbox
            string Str = textBox1.Text.Trim();
            double Num;
            bool isNum = double.TryParse(Str, out Num);

            if (isNum)
                Console.ReadLine();
            else
                MessageBox.Show("Enter A Valid ID Number!");
        }

        public void button2_Click(object sender, System.EventArgs e)
        {
            string text = textBox1.Text;
            Mainform = this;

            this.Hide();

            GetSchedule myScheduleFinder = new GetSchedule();
            string result = myScheduleFinder.GetDataFromNumber(text);

            if (!string.IsNullOrEmpty(result))
            {
                MessageBox.Show(result);
            }
            else
            {
                MessageBox.Show("Enter A Valid ID Number!");
            }
        }

        public void dateTimePicker1_ValueChanged(object sender, System.EventArgs e)
        {

        }

        public void pictureBox1_Click(object sender, System.EventArgs e)
        {

        }

        public void progressBar1_Click(object sender, EventArgs e)
        {
            //this.progressBar1 = new System.progressBar1();
            //progressBar1.Maximum = 200;
            //progressBar1.Minimum = 0;
            //progressBar1.Step = 20;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //if (progressBar1.Value >= 200 )
            //{
            //progressBar1.Value = 0;
            //}
            //return;
            //}

            //progressBar1.Value != 20;

        }
    }
}

Here is my form 2 class:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Eagle_Eye_Class_Finder
{
    /// <summary>
    /// Summary description for Form2.
    /// </summary>
    public class YOURCLASSSCHEDULE : System.Windows.Forms.Form
    {
        public System.Windows.Forms.LinkLabel linkLabel1;
        public System.Windows.Forms.LinkLabel linkLabel2;
        public System.Windows.Forms.LinkLabel linkLabel3;
        public System.Windows.Forms.LinkLabel linkLabel4;
        private Button button1;

        /// Required designer variable.

        public System.ComponentModel.Container components = null;

        public YOURCLASSSCHEDULE()
        {
            //
            InitializeComponent();

            // TODO: Add any constructor code after InitializeComponent call
        }

        /// Clean up any resources being used.
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(YOURCLASSSCHEDULE));
            this.linkLabel1 = new System.Windows.Forms.LinkLabel();
            this.linkLabel2 = new System.Windows.Forms.LinkLabel();
            this.linkLabel3 = new System.Windows.Forms.LinkLabel();
            this.linkLabel4 = new System.Windows.Forms.LinkLabel();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // linkLabel1
            //
            this.linkLabel1.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.linkLabel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.linkLabel1.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.linkLabel1.LinkArea = new System.Windows.Forms.LinkArea(0, 7);
            this.linkLabel1.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
            this.linkLabel1.Location = new System.Drawing.Point(41, 123);
            this.linkLabel1.Name = "linkLabel1";
            this.linkLabel1.Size = new System.Drawing.Size(288, 32);
            this.linkLabel1.TabIndex = 1;
            this.linkLabel1.TabStop = true;
            this.linkLabel1.Text = "Class 1";
            this.linkLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
            //
            // linkLabel2
            //
            this.linkLabel2.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.linkLabel2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.linkLabel2.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.linkLabel2.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
            this.linkLabel2.Location = new System.Drawing.Point(467, 123);
            this.linkLabel2.Name = "linkLabel2";
            this.linkLabel2.Size = new System.Drawing.Size(288, 32);
            this.linkLabel2.TabIndex = 2;
            this.linkLabel2.TabStop = true;
            this.linkLabel2.Text = "Class 2";
            this.linkLabel2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.linkLabel2.VisitedLinkColor = System.Drawing.Color.Navy;
            this.linkLabel2.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel2_LinkClicked);
            //
            // linkLabel3
            //
            this.linkLabel3.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.linkLabel3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.linkLabel3.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.linkLabel3.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
            this.linkLabel3.Location = new System.Drawing.Point(41, 311);
            this.linkLabel3.Name = "linkLabel3";
            this.linkLabel3.Size = new System.Drawing.Size(288, 32);
            this.linkLabel3.TabIndex = 3;
            this.linkLabel3.TabStop = true;
            this.linkLabel3.Text = "Class 3";
            this.linkLabel3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.linkLabel3.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel3_LinkClicked);
            //
            // linkLabel4
            //
            this.linkLabel4.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.linkLabel4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.linkLabel4.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.linkLabel4.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
            this.linkLabel4.Location = new System.Drawing.Point(467, 311);
            this.linkLabel4.Name = "linkLabel4";
            this.linkLabel4.Size = new System.Drawing.Size(288, 32);
            this.linkLabel4.TabIndex = 4;
            this.linkLabel4.TabStop = true;
            this.linkLabel4.Text = "Class 4";
            this.linkLabel4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.linkLabel4.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel4_LinkClicked);
            //
            // button1
            //
            this.button1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.button1.FlatAppearance.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(64)))));
            this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
            this.button1.Font = new System.Drawing.Font("Pristina", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button1.ForeColor = System.Drawing.SystemColors.ActiveCaption;
            this.button1.ImageAlign = System.Drawing.ContentAlignment.TopCenter;
            this.button1.Location = new System.Drawing.Point(358, 206);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(101, 25);
            this.button1.TabIndex = 5;
            this.button1.Text = "Go Back";
            this.button1.UseVisualStyleBackColor = false;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // YOURCLASSSCHEDULE
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
            this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.ClientSize = new System.Drawing.Size(790, 482);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.linkLabel4);
            this.Controls.Add(this.linkLabel3);
            this.Controls.Add(this.linkLabel2);
            this.Controls.Add(this.linkLabel1);
            this.Font = new System.Drawing.Font("OldDreadfulNo7 BT", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Name = "YOURCLASSSCHEDULE";
            this.Text = "Your Classes";
            this.Load += new System.EventHandler(this.Form2_Load);
            this.ResumeLayout(false);

        }
        #endregion

        public void Form2_Load(object sender, System.EventArgs e)
        {
           // if (text == "900456317")
           // {

            //}

        }

        public void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
        {

            System.Diagnostics.Process.Start("http://www.georgiasouthern.edu/map/");
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.georgiasouthern.edu/map/");
        }

        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.georgiasouthern.edu/map/");
        }

        private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.georgiasouthern.edu/map/");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();
            form1.Show();
            this.Hide();
        }
    }
}


The problem is on this if statement:

        IDnumber[] IDnumbers = new IDnumber[3];
        foreach (IDnumber IDCandidateMatch in IDnumbers)
        {

            if (IDCandidateMatch.ID == ID)

You created array with three elements, but all elements are null. When you loop through them, the reference to IDCandidateMatch.ID generates the NullReferenceException.


Compiling perfectly has nothing to do with Exceptions. I would suggest looking at the stacktrace on the Exception, trying to find the line where the error is occuring, and going from there.

It looks like line 24 of GetSchedule.cs is a good place to start!


You're creating 2 different arrays, one in the constructor and another one in the method. if you move the line IDnumber[] IDnumbers = new IDnumber[3]; outside of the method, and delete the identical one in the constructor it should work

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜