"Object reference not set to an instance of an object." in C#, SQL 2005, VS 2008
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);
connec开发者_如何学Gotion.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."
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>();
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;
}
}
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.
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.
精彩评论