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,905
  • 4
  • 21
  • 37
Clem
  • 11,334
  • 8
  • 34
  • 48

6 Answers6

7

You have to actually create List instance

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

In the first line.

Anri
  • 6,175
  • 3
  • 37
  • 61
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
  • 516
  • 5
  • 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
  • 54,668
  • 9
  • 68
  • 101
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
  • 16,183
  • 14
  • 60
  • 85
1

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

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
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,097
  • 11
  • 19