我希望动态创建对象并将其保存到列表中。当程序到达add
在列表中添加对象的方法时,我得到一个错误:
你调用的对象是空的。
我在这里做错了什么?
List<Category> categoryList;
public Main(string firstname, string lastname, string status)
{
InitializeComponent();
label1.Text = (firstname + lastname + status).Trim();
string connection = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Trgovina.mdf;Integrated Security=True";
SqlConnection cn = new SqlConnection(connection);
try
{
cn.Open();
}
catch (Exception) { MessageBox.Show("Error occurred during database communication!"); }
string sqlQuery = "SELECT * FROM Kategorije_art";
SqlCommand categoryCommand = new SqlCommand(sqlQuery, cn);
SqlDataReader categoryDataRead = categoryCommand.ExecuteReader();
categoryList.Add(new Category(1, "a")); //ERROR ?!
}