1

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 ?!
}
Marc
  • 3,739
  • 4
  • 19
  • 34
Clem
  • 9,476
  • 8
  • 31
  • 46

6 Answers6

7

You have to actually create List instance

List<Category> categoryList= new List<Category>();

In the first line.

Anri
  • 5,947
  • 3
  • 33
  • 59
4

Your member, categoryList, is not initialised.

Try:

List<Category> categoryList = new List<Category>();

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 ?!
}

Also note you should probably have your SqlConnection in a using block, or at least call Close/Dispose on it so you aren't leaking connections.

Cashley
  • 506
  • 4
  • 16
2

Your categoryList is not initialized. Change your first line to:

List<Category> categoryList = new List<Category>();

The null reference exception is coming when you try to access the .Add(...) method on a null value.

Aren
  • 50,600
  • 8
  • 63
  • 100
1

Your variable named categoryList is not initialized. So it is null.

You can't call any method on a unitialized variable.

Try :

List<Category> categoryList= new List<Category>();
xlecoustillier
  • 15,451
  • 14
  • 56
  • 79
1

do you create categoryList = new List<Category>()?

Aniket Inge
  • 24,028
  • 4
  • 42
  • 74
1

Your error line should look like this:

categoryList = new List<Category>() { new Category(1,"a")};

so You create list and add one created element and store reference to this list in categoryList

rumburak
  • 1,051
  • 9
  • 19