C# “对象引用未设置为对象的实例。”在C中,SQL 2005,VS 2008
我正在尝试从SQLServer2005中的食物表中选择食物项目名称和食物单价 我有以下代码:C# “对象引用未设置为对象的实例。”在C中,SQL 2005,VS 2008,c#,sql,C#,Sql,我正在尝试从SQLServer2005中的食物表中选择食物项目名称和食物单价 我有以下代码: private void GetDatabaseConnection() { string connectionString = @"Server = RZS-F839AD139AA\SQLEXPRESS; Integrated Security = SSPI; Database = HotelCustomerManagementDatabase"; connection = new S
private void GetDatabaseConnection()
{
string connectionString = @"Server = RZS-F839AD139AA\SQLEXPRESS; Integrated Security = SSPI; Database = HotelCustomerManagementDatabase";
connection = new SqlConnection(connectionString);
connection.Open();
}
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; }
}
}
及
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;
}
在食品类中,我有以下代码:
private void GetDatabaseConnection()
{
string connectionString = @"Server = RZS-F839AD139AA\SQLEXPRESS; Integrated Security = SSPI; Database = HotelCustomerManagementDatabase";
connection = new SqlConnection(connectionString);
connection.Open();
}
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; }
}
}
但它会产生以下异常。为什么?
对象引用未设置为对象的实例。是否查看堆栈跟踪以确定错误发生在哪一行或哪个函数上 从我看来,它可能是指PopulateFoodItemListview方法中的食物对象。食物对象永远不会被创建。好的开始: 连接字符串应进入app.config/web.config
ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString
使用以下代码打开/关闭连接:
using(SqlConnection connection = new SqlConnection(ConnectionString))
{
connection.Open();
}
并尽量避免读取器中出现空值
SELECT ISNULL(food_ItemName, ''), ISNULL(food_UnitPrice, 0.0)
编辑:
您忘记初始化类的私有成员
编辑这些行:
private List<string> itemName = new List<string>;
private List<double> unitPrice = new List<double>;
还是这样做
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;
}
}
它抱怨哪个变量
看起来是有问题的一个,可能还有更多的问题,但这一个肯定会导致异常是ItemName,它没有启动。您需要创建'ItemName'和'unitPrice'变量的实例,然后才能添加到其中任何一个。您可以在声明它们时继续这样做
private List<string> itemName = new List<string>();
private List<double> unitPrice = new List<string>();
尝试调试并查看代码在哪一行中断。对不起,我忘了提及。但现在我在这一行添加了一条注释,我声明了Food-Food=新食品,在方法之外,但在类内部。所以食物是全球性的。谢谢Mike737花了你宝贵的时间给我。我声明连接是全球性的。在你的代码中,如果抛出异常,连接不会关闭。因此,你必须在对象所在的上下文中确定这一点。。。