我下面這個視頻:http://www.youtube.com/watch?v=Y63vq_tcTGk,並在8:20他類型的NullReferenceException是未處理#2
ds.Add(a);
show_diem();
但是一個錯誤出現,當我做ds.Add(一);
我是新來的,所以我還不確定ds是什麼?它可以是任何東西嗎?他宣稱
ds = new ArrayList();
在一開始所以它可以是什麼?爲什麼會出現錯誤?這是我的Form1上的代碼。 「員工」是一個類,「employeeId」「firstName」等是文本框。我用「em」而不是「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();
}
}
}
錯誤說:對象引用未設置爲對象的實例。
你確定你的Form1_Load事件被調用嗎? – sgmoore 2012-04-16 17:30:24
該視頻中使用的做法很糟糕。 – 2012-04-16 17:37:17
@sgmoore OH事實證明這是不正確的,但現在我已經重做了它,它的工作原理,但現在GUI有不同的問題。當我點擊按鈕時,只顯示名字值。有任何想法嗎? – user1336868 2012-04-16 17:41:17