我正在从事电子商务项目,并且与读者有点麻烦。这是我的ShoppingCartController代码无法正常工作
public List<PRODUCT> GetCartItems()
{
SqlConnection sqlConnection1 = new SqlConnection("MyConnection");
SqlCommand cmd = new SqlCommand("uspGetCart", sqlConnection1);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Username", SqlDbType.VarChar).Value = User.Identity.GetUserName();
sqlConnection1.Open();
SqlDataReader reader = cmd.ExecuteReader();
List<PRODUCT> cartList = new List<PRODUCT>();
PRODUCT product;
while (reader.Read())
{
product = new PRODUCT();
product.MANUFACTURER.ManufacturerName = reader["ManufacturerName"].ToString();
product.Name = reader["Name"].ToString();
product.Cost = decimal.Parse(reader["Cost"].ToString());
cartList.Add(product);
}
sqlConnection1.Close();
return cartList;
}
我得到的错误是 对象引用未设置为对象的实例。
Stack Trace是
[NullReferenceException: Object reference not set to an instance of an object.]
SeeSharpBeans.Controllers.ShoppingCartController.GetCartItems() in c:\Users\Andrew\Documents\Visual Studio 2013\Projects\SeeSharpBeans\SeeSharpBeans\Controllers\ShoppingCartController.cs:54
SeeSharpBeans.Controllers.ShoppingCartController.Index() in c:\Users\Andrew\Documents\Visual Studio 2013\Projects\SeeSharpBeans\SeeSharpBeans\Controllers\ShoppingCartController.cs:22
lambda_method(Closure , ControllerBase , Object[] ) +101
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +59
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +435
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +60
System.Web.Mvc.Async.ActionInvocation.InvokeSynchronousActionMethod() +76
System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +36
System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +73
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +117
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +323
System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +44
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +72
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +37
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
我的产品型号如下所示
public partial class PRODUCT
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public PRODUCT()
{
this.PURCHASES = new HashSet<Purchase>();
this.TRANSACTIONS = new HashSet<TRANSACTION>();
}
public int ProductID { get; set; }
public int ManufacturerID { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
public decimal Cost { get; set; }
public virtual MANUFACTURER MANUFACTURER { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Purchase> PURCHASES { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<TRANSACTION> TRANSACTIONS { get; set; }
}
答案 0 :(得分:2)
如果您的数据不包含空值,那么可能的问题是您没有将MANUFACTURER
属性初始化为PRODUCT
构造函数中的值,这意味着当您尝试设置时它为null product.MANUFACTURER.ManufacturerName
到一个值。
另请注意,ALL CAPS中的命名类和成员通常是错误的形式 - 请考虑将其更改为Product
和Manufacturer
。
答案 1 :(得分:2)
我认为问题可能出在
行product.MANUFACTURER.ManufacturerName = reader["ManufacturerName"].ToString();
MANUFACTURER是否已在Product类中初始化?
答案 2 :(得分:0)
尝试将集合项定义移动到while循环的范围内:
while (reader.Read())
{
PRODUCT product = new PRODUCT();
product.MANUFACTURER.ManufacturerName = reader["ManufacturerName"].ToString();
product.Name = reader["Name"].ToString();
product.Cost = decimal.Parse(reader["Cost"].ToString());
cartList.Add(product); }
答案 3 :(得分:0)
This line is the issue
product.MANUFACTURER.ManufacturerName = reader["ManufacturerName"].ToString();
Use
product.MANUFACTURER.ManufacturerName = Convert.ToString(reader["ManufacturerName"])
product.Cost = decimal.Parse(Convert.ToString(reader["Cost"]));
instead of .ToString();
答案 4 :(得分:0)
需要使用以下构造函数初始化MANUFACTURER: -
while (reader.Read())
{
product = new PRODUCT();
product.MANUFACTURER=new MANUFACTURER();
product.MANUFACTURER.ManufacturerName = reader["ManufacturerName"].ToString();
product.Name = reader["Name"].ToString();
product.Cost = decimal.Parse(reader["Cost"].ToString());
cartList.Add(product);
}