0

我正在从事一个电子商务项目,但在阅读器方面遇到了一些麻烦。这是我的 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;
    }

我得到的错误是对象引用未设置为对象的实例。

堆栈跟踪是

    [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; }
}
4

5 回答 5

2

如果您的数据不包含空值,那么可能的问题是您没有将MANUFACTURER属性初始化为PRODUCT构造函数中的值,这意味着当您尝试设置product.MANUFACTURER.ManufacturerName为值时它为空。

另请注意,以 ALL CAPS 命名类和成员通常是不好的形式 - 考虑将它们更改为Productand Manufacturer

于 2015-11-26T04:00:42.003 回答
2

我认为问题可能出在线路上

product.MANUFACTURER.ManufacturerName = reader["ManufacturerName"].ToString();

MANUFACTURER 是否在 Product 类中初始化?

于 2015-11-26T04:02:15.870 回答
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); }
于 2015-11-26T04:47:03.617 回答
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);
            }
于 2015-11-26T06:24:40.277 回答
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();
于 2015-11-26T04:54:25.430 回答