I wish to dynamically create and save objects to a list. When program reachs the add
method to add objects in the list I get an error:
Object reference not set to an instance of an object.
What have I done wrong here?
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 ?!
}