2

I am trying to select "food_ItemName" and "food_UnitPrice" from "t_Food" table in SQL Server 2005.

I have the following code:

private void GetDatabaseConnection()
{
    string connectionString = @"Server = RZS-F839AD139AA\SQLEXPRESS; Integrated Security = SSPI; Database = HotelCustomerManagementDatabase";
    connection = new SqlConnection(connectionString);
    connection.Open();
}

and.....

public Food PopulateFoodItemListview()
{
    GetDatabaseConnection();
    string selectFoodItemQuery = @"SELECT food_ItemName, food_UnitPrice FROM t_Food";
    SqlCommand command = new SqlCommand(selectFoodItemQuery, connection);
    SqlDataReader reader = command.ExecuteReader();

    while (reader.Read())
    {
        food.ItemName.Add(reader.GetString(0));  // Exception is generated in this line
        food.UnitPrice.Add(reader.GetDouble(1));
    }
    connection.Close();
    return food;
}

In food class I have the following code:

public class Food
{
    private List<string> itemName;
    private List<double> unitPrice;
    private double itemUnit;
    private Customer foodCustomer = new Customer();

    public Food ()
    {
    }

    public Food(List<string> itemName, List<double> unitPrice) : this()
    {
        this.itemName = itemName;
        this.unitPrice = unitPrice;
    }

    public List<string> ItemName
    {
        get { return itemName; }
        set { itemName = value ; }
    }

    public List<double> UnitPrice
    {
        get { return unitPrice; }
        set { unitPrice = value; }
    }

    public double ItemUnit
    {
        get { return itemUnit; }
        set { itemUnit = value; }
    }
}

but it generating following exception. Why?

"Object reference not set to an instance of an object."

2
  • Try to debug and see on which line the code breaks. Commented Aug 19, 2010 at 11:35
  • Sorry I forget to mention. But now I added a comment on that line. Commented Aug 19, 2010 at 11:41

4 Answers 4

2

You need to create an instance of the 'itemName' and 'unitPrice' variables before you can add to either of them. You can go ahead and do that when you are declaring them.

private List<string> itemName = new List<string>();
private List<double> unitPrice = new List<string>();
Sign up to request clarification or add additional context in comments.

Comments

2

Well to start:

ConnectionStrings should go into the app.config/web.config.

ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString

Use the following code to open/close connection:

using(SqlConnection connection = new SqlConnection(ConnectionString))
{
  connection.Open();

}

And try to avoid having nulls in the reader...

SELECT ISNULL(food_ItemName, ''), ISNULL(food_UnitPrice, 0.0) 

EDIT:

You forgot to initialize the private member of the class Food

Edit to those lines:

private List<string> itemName = new List<string>; 
private List<double> unitPrice = new List<double>; 

Or do it like this

public List<string> ItemName 
{ 
   get 
   { 
     if (itemName == null) itemName = new List<string>; 
     return itemName; 
   } 
}


public List<double> UnitPrice 
{ 
  get 
  { 
    if (unitPrice== null) unitPrice= new List<double>; 
    return unitPrice; 
  } 
} 

2 Comments

I declared "Connection" as global.
In your code the connection is not closed if an exception is thrown. So you have to make sure about this in the context the object lives in...
1

On which variable does it complain?

It looks like the problematic one (well, maybe there are more problems but this one is for sure can cause the exception) is ItemName which is not initiated.

Comments

0

Have you looked at your stack trace to figure out which line the error is occurring on or which function?

From what I could see it look like it may be referring to your food object in the PopulateFoodItemListview() method. The food object is never created.

2 Comments

I declared Food food = new Food() outside of method but inside in class. So "food" is global.
Thanks Mike737 for spending your valuable time to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.