I'm having an issue with deleting an object now, earlier this week this code was working fine, but now I'm getting a null reference exception, even though the object I'm attempting to delete, and the instance of the entity framework are not null.
MHNHubEntities _entities = new MHNHubEntities();
// Get, GetList, Add, Save etc.
public void Delete(PackageProduct packageProduct)
{
_entities.PackageProducts.DeleteObject(packageProduct);
}
packageProduct is a valid packageProduct, and everything else but this delete works. Normally I wouldn't ask how to solve a null reference exception - because its fairly obvious, check for nulls. However - in this case I'm stumped, what was working yesterday suddenly isn't and this is whats throwing the exception. Any help would be appreciated, I've already confirmed there are no null objects involved when this exception is thrown.
edit
I don't want to rule out that something is null and causing this - because of the very nature of the exception being thrown. As per request, here is the stack trace:
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)
Heres my controller action:
[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);
}
}
Deletecalled from a finalizer? Because that could cause a null reference exception.