2

I have below Model

public class UploadImageAlbum
{
    public UploadImageAlbum()
    {
       Albums = new SelectList(Enumerable.Empty<SelectListItem>());
    }
    public List<HttpPostedFileBase> Images { get; set; }

    public string AlbumID { get; set; }
    public SelectList Albums { get; set; }
}

public class Album
{
   public string AlbumID { get; set; }
   public string AlbumName { get; set; }
}

public class Repository
{
   public static List<Album> FetchAlbums()
   {
        List<Album> albums = new List<Album>
        {
            new Album(){ AlbumID = "Album1", AlbumName = "Album 1 desc"},
            new Album(){ AlbumID = "Album2", AlbumName = "Album 2 desc"}
        };
        return albums;
    }
}

and I call this UploadImageAlbum in a separate model called AdminViewModel as below:

public class AdminViewModel
{
    public UploadImageAlbum UIAModel { get; set; }
}

and in Controller I try to fill this model as below:

public PartialViewResult GetMediaUploadView()
{
   AdminViewModel model = new AdminViewModel();
   List<Album> albums = Repository.FetchAlbums();
   model.UIAModel.Albums = new SelectList(albums, "AlbumID", "AlbumName");//Error here
   //Object reference not set to an instance of the object
   return PartialView("_UploadMedia", model);
}

I also tried

public PartialViewResult GetMediaUploadView()
{
   AdminViewModel model = new AdminViewModel();
        List<Album> albums = new List<Album>(); //Creating as new List
        albums = Repository.FetchAlbums();//then assign
        model.UIAModel.Albums = new SelectList(albums, "AlbumID", "AlbumName");
        return PartialView("_UploadMedia", model);
}

But again, I am getting same error. No idea for what reason I am getting this error, even after initializing in model level as well as Controller level. Hope someone might have good knowledge on this. Below is the screen shot of the error I am getting.

Screenshot

Update

Well I don't think this is the duplicate, as I am too aware that there are 'n' number of questions based on null reference, But the level where we get that null reference is what makes this question unique. Also I was in impression that, the SelectList was throwing error, turned out the problem to be different here. So I stand by with my question

Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200
  • 1
    Looks like `model.UIAModel` is `null` – Ivan Stoev Oct 22 '15 at 18:06
  • 1
    `model.UIAModel` looks `null` to me. so `model.UIAModel.Albums` is the real error thrower here. try create an object in constructor of `AdminViewModel` for `UIAModel`. – Ergun Ozyurt Oct 22 '15 at 18:12
  • @M.kazemAkhgary Well people often think seeing the title, but if you read the question, then you wouldn't have though so.. :) The Solution might be general, **Something is null and not initialized**, but which hierarchy it comes up to be `null` is what makes question unique.. :) Anyways, I stand by my point and hope this helps someone in future.. :) Happy coding.. :) – Guruprasad J Rao Oct 22 '15 at 18:16
  • @GuruprasadRao The duplicate is about how to determine exactly what is `null` which was your main issue. If you had known that it was `model.UIAModel` and not just `model` then the solutions should have been more obvious. – juharr Oct 22 '15 at 18:55

1 Answers1

4

Your AdminViewModel has a property:

public class AdminViewModel
{
    public UploadImageAlbum UIAModel { get; set; }
}

then:

AdminViewModel model = new AdminViewModel();
//..
model.UIAModel.Albums = //...

you haven't initialized UIAModel, he's throwing the error.


If you're in C#6 you can simply use:

public class AdminViewModel
{
    public UploadImageAlbum UIAModel { get; set; } = new UploadImageAlbum();
}

for a quick fix

Jonesopolis
  • 25,034
  • 12
  • 68
  • 112