none
How do I fix this? - NullReferenceException - Object reference not set to an instance of an object.

    Question

  • There needs to be 10 employee slots but only 6 are being used at the moment.

    Form1.cs

    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;
    
    namespace FluffShuffle
    {
        public partial class Form1 : Form
        {
            int employnum;
            string name;
            string address;
            double wage;
            double hour;
            string[] wh = new string[2];
    
            StreamReader reader = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\" + "Data.txt");
    
            int i = 1;
    
            Employee employee1;
            Employee employee2;
            Employee employee3;
            Employee employee4;
            Employee employee5;
            Employee employee6;
            Employee employee7;
            Employee employee8;
            Employee employee9;
            Employee employee10;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                ReadIn();
                employee1 = new Employee(employnum, name, address, wage, hour);
    
                txtname.Text = employee1.GetName();
                txtaddress.Text = employee1.GetAddress();
                txtpay.Text = employee1.GetPay().ToString();
    
                ReadIn();
                employee2 = new Employee(employnum, name, address, wage, hour);
                ReadIn();
                employee3 = new Employee(employnum, name, address, wage, hour);
                ReadIn();
                employee4 = new Employee(employnum, name, address, wage, hour);
                ReadIn();
                employee5 = new Employee(employnum, name, address, wage, hour);
                ReadIn();
                employee6 = new Employee(employnum, name, address, wage, hour);
                ReadIn();
                employee7 = new Employee(employnum, name, address, wage, hour);
                ReadIn();
                employee8 = new Employee(employnum, name, address, wage, hour);
                ReadIn();
                employee9 = new Employee(employnum, name, address, wage, hour);
                ReadIn();
                employee10 = new Employee(employnum, name, address, wage, hour);
    
                
            }
            private void ReadIn()
            {
                employnum = int.Parse(reader.ReadLine());
                name = reader.ReadLine();
                address = reader.ReadLine();
                wh = reader.ReadLine().Split();
                wage = double.Parse(wh[0]);
                hour = double.Parse(wh[1]);
            }
    
            private void btnnext_Click(object sender, EventArgs e)
            {
                i++;
                if (i == 2)
                {
                    txtname.Text = employee2.GetName();
                    txtaddress.Text = employee2.GetAddress();
                    txtpay.Text = employee2.GetPay().ToString();
                }
                else if (i == 3)
                {
                    txtname.Text = employee3.GetName();
                    txtaddress.Text = employee3.GetAddress();
                    txtpay.Text = employee3.GetPay().ToString();
                }
                else if (i == 4)
                {
                    txtname.Text = employee4.GetName();
                    txtaddress.Text = employee4.GetAddress();
                    txtpay.Text = employee4.GetPay().ToString();
                }
                else if (i == 5)
                {
                    txtname.Text = employee5.GetName();
                    txtaddress.Text = employee5.GetAddress();
                    txtpay.Text = employee5.GetPay().ToString();
                }
                else if (i == 6)
                {
                    txtname.Text = employee6.GetName();
                    txtaddress.Text = employee6.GetAddress();
                    txtpay.Text = employee6.GetPay().ToString();
                }
                else if (i == 7)
                {
                    txtname.Text = employee7.GetName();
                    txtaddress.Text = employee7.GetAddress();
                    txtpay.Text = employee7.GetPay().ToString();
                }
                else if (i == 7)
                {
                    txtname.Text = employee8.GetName();
                    txtaddress.Text = employee8.GetAddress();
                    txtpay.Text = employee8.GetPay().ToString();
                }
                else if (i == 9)
                {
                    txtname.Text = employee9.GetName();
                    txtaddress.Text = employee9.GetAddress();
                    txtpay.Text = employee9.GetPay().ToString();
                }
                else if (i == 10)
                {
                    txtname.Text = employee10.GetName();
                    txtaddress.Text = employee10.GetAddress();
                    txtpay.Text = employee10.GetPay().ToString();
                }
                else if (i >= 11)
                {
                    MessageBox.Show("Maximum Employees Allowed.");
                }
            }
    
            private void exitToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }


    Employee.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace FluffShuffle
    {
        class Employee
        {
            private int employnum;
            private string name;
            private string address;
            private double wage;
            private double hour;
    
            public Employee(int a, string b, string c, double d, double e)
            {
                employnum = a;
                name = b;
                address = c;
                wage = d;
                hour = e;
            }
    
            public int GetEmployeeNumber()
            {
                return employnum;
            }
    
            public string GetName()
            {
                return name;
            }
    
            public string GetAddress()
            {
                return address;
            }
    
            public double GetWage()
            {
                return wage;
            }
    
            public double GetHour()
            {
                return hour;
            }
    
            public double GetPay()
            {
                return wage * hour;
            }
        }
    }

    Data.txt

    1
    John MerryWeather
    123 West Main Street
    5.00 30
    2
    Andrew Buttons
    17 East Riverview Drive
    12.00 40
    3
    Martha Washington
    1 Mount Vernon Lane
    7.25 20
    4
    Harry Skilling
    786 No. Rodeo Drive
    8.00 45
    5
    Ann Mindbender
    192 Wizard Street
    9.00 40
    6
    Carl Zabriskie
    42 No. State Street
    10.50 42
    • Edited by aileron565 Friday, May 03, 2013 1:38 AM
    Friday, May 03, 2013 1:29 AM

Answers

  • Your problem is that ReadLine returns null when it hits the end of the file, so all the data after employee6 is null.

    You should really consider using an array or list for the employees, instead of giving them separately named variables. Then you could just stop at the end of the array and avoid the current problem.

    However, the simplest way to avoid the error with the current code is probably something like so...

            private void ReadIn()
            {
                string number = reader.ReadLine();
                if(number != null)
                {
                   // There was an employee in the file, so use it.
                   employnum = int.Parse(number);
                   name = reader.ReadLine();
                   address = reader.ReadLine();
                   wh = reader.ReadLine().Split();
                   wage = double.Parse(wh[0]);
                   hour = double.Parse(wh[1]);
                }
                else
                {
                   // This employee isn't in the file, so just use some empty defaults.
                   employnum = -1;
                   name = " ";
                   address = " ";
                   wage = 0.0;
                   hour = 0.0;
                }
            }

    Oh, and you have the following line twice...

                else if (i == 7)

    ... so employee8 won't be initialised.

    Friday, May 03, 2013 3:17 AM
  • My proposal is to read your employee data into a List<Employee>. The advantage would be that you could refer later to the index of each Employee object within this list, esp. within your btnnext_Click method. So you could do something like this:

    // in Form1_Load or while Initializing. List<Employee> employees = ReadIn(); private void btnnext_Click(object sender, EventArgs e) { i++; // I suppose i is not zero based.

    if (i <= employees.Length) { txtname.Text = employees[i].GetName(); txtaddress.Text = employees[i].GetAddress(); txtpay.Text = employees[2].GetPay().ToString(); }... } private List<Employee> ReadIn() { List<Employee> lst = new List<Employee>(); using (StreamReader sr = new StreamReader(path)) { while (sr.Peek() >= 0) { int employnum = Convert.ToInt32(sr.ReadLine()); string name = sr.ReadLine(); string address = sr.ReadLine(); string[] wh = sr.ReadLine().Split(); string wage = Convert.ToDouble(wh[0]); string hour = Convert.ToDouble(wh[1]); lst.Add(new Employee(employnum, name, address, wage, hour)); } } return lst; }

    wizend

    Friday, May 03, 2013 7:22 PM

All replies

  • At which statement?

    Debug and test what has caused such an instance null?


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    Friday, May 03, 2013 1:33 AM
  • It is at "txtname.Text = employee7.GetName();"

    Friday, May 03, 2013 1:41 AM
  • Your problem is that ReadLine returns null when it hits the end of the file, so all the data after employee6 is null.

    You should really consider using an array or list for the employees, instead of giving them separately named variables. Then you could just stop at the end of the array and avoid the current problem.

    However, the simplest way to avoid the error with the current code is probably something like so...

            private void ReadIn()
            {
                string number = reader.ReadLine();
                if(number != null)
                {
                   // There was an employee in the file, so use it.
                   employnum = int.Parse(number);
                   name = reader.ReadLine();
                   address = reader.ReadLine();
                   wh = reader.ReadLine().Split();
                   wage = double.Parse(wh[0]);
                   hour = double.Parse(wh[1]);
                }
                else
                {
                   // This employee isn't in the file, so just use some empty defaults.
                   employnum = -1;
                   name = " ";
                   address = " ";
                   wage = 0.0;
                   hour = 0.0;
                }
            }

    Oh, and you have the following line twice...

                else if (i == 7)

    ... so employee8 won't be initialised.

    Friday, May 03, 2013 3:17 AM
  • My proposal is to read your employee data into a List<Employee>. The advantage would be that you could refer later to the index of each Employee object within this list, esp. within your btnnext_Click method. So you could do something like this:

    // in Form1_Load or while Initializing. List<Employee> employees = ReadIn(); private void btnnext_Click(object sender, EventArgs e) { i++; // I suppose i is not zero based.

    if (i <= employees.Length) { txtname.Text = employees[i].GetName(); txtaddress.Text = employees[i].GetAddress(); txtpay.Text = employees[2].GetPay().ToString(); }... } private List<Employee> ReadIn() { List<Employee> lst = new List<Employee>(); using (StreamReader sr = new StreamReader(path)) { while (sr.Peek() >= 0) { int employnum = Convert.ToInt32(sr.ReadLine()); string name = sr.ReadLine(); string address = sr.ReadLine(); string[] wh = sr.ReadLine().Split(); string wage = Convert.ToDouble(wh[0]); string hour = Convert.ToDouble(wh[1]); lst.Add(new Employee(employnum, name, address, wage, hour)); } } return lst; }

    wizend

    Friday, May 03, 2013 7:22 PM
  • Thanks Ante and Wizend!
    Friday, May 03, 2013 9:50 PM