Frage

I was following this video: http://www.youtube.com/watch?v=Y63vq_tcTGk and at 8:20 he types

    ds.Add(a);
    show_diem();

however an error comes up when I do ds.Add(a);

I'm new to this so I'm still unsure of what ds is? Can it be anything? He declares

   ds = new ArrayList();

at the beginning so can it be anything? And why is the error popping up? Here is the code on my Form1. "Employee" is a class and "employeeId" "firstName" etc are textboxes. I used "em" instead of "ds".

    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.Collections;


    namespace Employee_Program
    {
        public partial class Form1 : Form
        {


    public Form1()
    {
        InitializeComponent();

    }

    public ArrayList em;

    private void Form1_Load(object sender, EventArgs e)
    {
        em = new ArrayList();
    }

    private void show_employee()
    {
        listView1.Items.Clear();
        foreach(Employee a in em)
        {
            int i = listView1.Items.Count;
            listView1.Items.Add(a.FirstName);
            listView1.Items[i].SubItems.Add(a.LastName);
            listView1.Items[i].SubItems.Add(a.EmployeeId.ToString());
            listView1.Items[i].SubItems.Add(a.YearSalary.ToString());

}
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Employee a = new Employee();
        a.FirstName = firstName.Text;
        a.LastName = lastName.Text;
        a.EmployeeId = float.Parse(employeeId.Text);
        a.YearSalary = float.Parse(yearSalary.Text);
        em.Add(a);
        show_employee();

            }
        }
    }

the error says: Object reference not set to an instance of an object.

War es hilfreich?

Lösung

It looks to me like you are writing the code to mimic the video, however a designer may have been used to wire up Form1_Load. So, you need to make sure that this is wired to your form. If it is not, then the initialization for the ArrayList will never be called.

If you go into the designer, you can click on the Form. Then in the Properties window, choose the Events tab. Find the OnLoad (or Load I am not sure on the naming) event. Then in the dropdown, your method should appear as the parameter definitions will match. Choose this, and the designer will wire this up.

The wiring should look something like

form.Load += Form1_Load;

Alternatively, you could accomplish the same by removing it from the FormLoad by doing this in the declaration:

public ArrayList em = new ArrayList();

Last, you could just do this in the Form constructor\

public Form1()
{
    em = new ArrayList();
    InitializeComponent();
}

Andere Tipps

You are tring to use em when it is null. One way to fix it is:

public Form1() 
{ 
    em = new ArrayList();  
    InitializeComponent(); 
} 

However, you may have another problem that the Form1_Load event is not wired up.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top