0

When I tried deployment my project on server I began get follow error. It inexplicable behavior for me, because I do similar action between my local computer and server.

[NullReferenceException: Object reference not set to an instance of an object.] MyProject.Models.ViewModels.CashboxViewModel..ctor(Cashbox entity) +168 MyProject.Controllers.CashboxController.Index(Nullable1 id) +194 lambda_method(Closure , ControllerBase , Object[] ) +150 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +241 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) +38 System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +11 System.Web.Mvc.Async.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) +138 System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3d() +111 System.Web.Mvc.Async.<>c__DisplayClass46.b__3f() +452 System.Web.Mvc.Async.<>c__DisplayClass33.b__32(IAsyncResult asyncResult) +15 System.Web.Mvc.Async.<>c__DisplayClass2b.b__1c() +37 System.Web.Mvc.Async.<>c__DisplayClass21.b__1e(IAsyncResult asyncResult) +241 System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +111 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +19 System.Web.Mvc.MvcHandler.b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +51 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

Controller

[HttpGet]
public ActionResult Index(Guid? id)
{
    Cashbox obj = null;
    if (id != null)
    {
        obj = service.GetById(id.Value);
    }

    var model = (TempData[SESSION_KEYS.Transfer] as CashboxViewModel) ?? new CashboxViewModel(obj);
    SetReferenceList(model);

    return View(model);
}

    public BaseViewModel(TEntity obj)
    {
        Set(obj);
    }

    protected void Set(TEntity obj, params string[] constrains)
    {
        if (obj != null)
        {
            foreach (var info in obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(x => x.CanRead))
            {
                if (constrains.Contains(info.Name))
                {
                    continue;
                }

                var piDest = this.GetType().GetProperty(info.Name);
                if (piDest != null && piDest.CanWrite)
                {
                    piDest.SetValue(this, info.GetValue(obj));
                }
            }
        }
    }

public class CashboxViewModel: BaseViewModel<Cashbox>
{

    public CashboxViewModel()
        : base()
    {

    }

    public CashboxViewModel(Cashbox entity)
        : base(entity)
    {

    }
}
Adil
  • 124
  • 7
  • Stephen Muecke, thanks mate for correct it. – Adil Apr 11 '18 at 03:41
  • The message is telling you that `obj` is `null` (when you call `new CashboxViewModel(obj)`) –  Apr 11 '18 at 03:44
  • Stephen Muecke, you know I feeling that my situation is not duplicate. Please come back my question. – Adil Apr 11 '18 at 03:44
  • Only you can debug your code (we do not even know what your constructor is for `CashboxViewModel`) All we can do it point you in the right direction. –  Apr 11 '18 at 03:46
  • If I had bad code then doesn't work anywhere – Adil Apr 11 '18 at 03:46
  • You do have bad code. If a user navigates to that method and `id` is `null` and the `TempData` value is `null`, then your code throws an exception. If it does have a value and you do execute `GetById()` - then what does that return if its not a valid `Guid` - I assume `null` so again you would throw and exception. Your code need to check for `null` –  Apr 11 '18 at 03:54
  • So, you think that the problem is here (TempData[SESSION_KEYS.Transfer] as CashboxViewModel) ? – Adil Apr 11 '18 at 03:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168697/discussion-between-stephen-muecke-and-adil). –  Apr 11 '18 at 03:59

0 Answers0