c# – 对象引用未设置为对象的实例……但它是?

前端之家收集整理的这篇文章主要介绍了c# – 对象引用未设置为对象的实例……但它是?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我现在有一个删除对象的问题,本周早些时候这段代码工作正常,但现在我得到一个空引用异常,即使我试图删除的对象,以及实体框架的实例不是空的.
MHNHubEntities _entities = new MHNHubEntities();

    // Get,GetList,Add,Save etc.

    public void Delete(PackageProduct packageProduct)
    {
        _entities.PackageProducts.DeleteObject(packageProduct);
    }

packageProduct是一个有效的packageProduct,除此删除之外的所有其他内容都有效.通常我不会问如何解决空引用异常 – 因为它非常明显,检查空值.然而 – 在这种情况下,我很难过,昨天突然发生的事情不是,这就是抛出异常的原因.任何帮助将不胜感激,我已经确认抛出此异常时不涉及空对象.

编辑

我不想排除某些东西是空的并导致这种情况 – 因为抛出异常的本质.根据请求,这里是堆栈跟踪:

at MHNHub.Areas.Store.Controllers.SettingsController.DeletePackage(Int32 id,FormCollection collection) in C:\Users\Grahame\Desktop\Projects\MHN Hub\Visual Studio Project\MHNHub\MHNHub\Areas\Store\Controllers\SettingsController.cs:line 618
   at lambda_method(Closure,ControllerBase,Object[] )
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller,Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext,ActionDescriptor actionDescriptor,IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter,ActionExecutingContext preContext,Func`1 continuation)

继承我的控制器动作:

[HttpGet]
        public ActionResult DeletePackage(int id,FormCollection collection)
        {
            try
            {

                IPackageRepository packageRepository = new PackageRepository();
                var package = packageRepository.GetPackage(id);

                IPackageProductRepository packageProductRepository = new PackageProductRepository();
                var packageProducts = package.PackageProducts.ToList();

                foreach (var packageProduct in packageProducts)
                {
                    packageProductRepository.Delete(packageProduct);
                }

                packageRepository.Delete(package);
                packageRepository.Save();

                return Json(new { Success = "true" },JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                //return JSON with exception
                var result = new { Message = ex.Message,InnerException = ex.InnerException.ToString() };

                return Json(result,JsonRequestBehavior.AllowGet);
            }
        }

解决方法

好吧,如果它正在这条线上:
_entities.PackageProducts.DeleteObject(packageProduct);

并假设这不是由于内联DeleteObject(即错误实际存在于那里),那么NullReferenceException只有两个可能的原因:

> _entities为null
> _entities.PackageProducts为null

如果这种情况可重复发生,应该很容易找出它是什么:

public void Delete(PackageProduct packageProduct)
{
    var x = _entities;
    if (x == null)
    {
        throw new Exception("_entities was null");
    }
    var y = x.PackageProducts;
    if (y == null)
    {
        throw new Exception("_entities.PackageProducts was null");
    }
    y.DeleteObject(packageProduct);
}

(当然,仅用于诊断目的.)

原文链接:https://www.f2er.com/csharp/100357.html

猜你在找的C#相关文章