Search code examples
asp.net-mvclinqnullreferenceexceptionasp.net-mvc-viewmodelpagedlist

Null Reference Error when directly assigning a List object to an PagedList


KeyMaster Model

        public class KeyMasterModel
        {   
            public int KeyId { get; set; }

            public int TypeId { get; set; }

            public string TypeName { get; set;}

        }

PagedKeyModel

        public class PagedKeyModel
        {
            // Collection of our KeyMasterModel , trying to populate this and send it to ViewModel to display a Grid//

            public IPagedList<Security.Models.KeyMasterModel> pagedkeymaster;
        }

//ViewModel//

           public class KeyMasterViewModel
        {

//tell me how to initialise the KeyMasterViewModel.pagedkeymodel.pagedkeymaster in a constructor here so that I dont get a null

            public PagedKeyModel pagedkeymodel;

            public KeyMasterModel keymastermodel;

        }

//Controller//

                 [HttpGet]
                public ActionResult ListOfKey(string sortOrder, string CurrentSort, int? page)
                {    

// View Model Object //

KeyMasterViewModel keyMasterViewModel = new KeyMasterViewModel();


    // At the gollowing step keyMasterModelObject has values retrieved from db //

//Next aim is to place it into ViewModel Object by assigning the retrieved PagedList of KeyMasterModel to IPagedList<Security.Models.KeyMasterModel> pagedkeymaster


  IPagedList<KeyMasterModel> KeyMasterModelObject= datalayercall.GetAll(sortOrder, CurrentSort, page);

    // Here is where error is thrown , all of a sudden I get the error , KeyMasterModelObject becomes null .

    // I am trying finally to populate everything into ViewModel object

          keyMasterViewModel .pagedkeymodel.pagedkeymaster = KeyMasterModelObject;

          return View(keyMasterViewModel);
        }

// Business Logic Layer

         public IPagedList<KeyMasterModel> GetAll(string sortOrder, string CurrentSort, int? page)
                {

                    var retrieveddatalayerobject = datalayerobject.KeyMasters;

    // Retrieving Data from db and forming a list according to my model in App//

                    List<KeyMasterModel> keymastermodellist = new List<KeyMasterModel>();

                    KeyMasterModel keymastermodelobject=new KeyMasterModel();

                    foreach(var retrieveditems in retrieveddatalayerobject)
                    {
                        keymastermodelobject.KeyId = retrieveditems.KeyId;
                        keymastermodelobject.TypeName = retrieveditems.TypeMaster.TypeName;

// Create a New List Of KeyMasterModel //

                       keymastermodellist.Add(keymastermodelobject);
                    }

    // Paged List of KeyMaster Model //

                    IPagedList<KeyMasterModel> IPagedListKeyMasterModel = null; 

                    switch (sortOrder)
                    {

                        case "KeyId":
                            if (sortOrder.Equals(CurrentSort))

                                IPagedListKeyMasterModel = keymastermodellist.OrderByDescending
                                        (m => m.KeyId).ToPagedList(pageIndex, pageSize);

                    }

                    return IPagedListKeyMasterModel;

                }

Solution

  • Your code as I understand it:

    KeyMasterViewModel keyMasterViewModel = new KeyMasterViewModel();
    [...]
    keyMasterViewModel.pagedkeymodel.pagedkeymaster = KeyMasterModelObject;
    

    So here you are using keyMasterViewModel.pagedkeymodel and assuming it is not null (because you are trying to assign to a property on it). However KeyMasterViewModel doesn't have a constructor and doesn't set pagedkeymodel to anything at declaration so it will default to being null. Hence your problem.