1
public class kingdomAddModel
{
    public string title { get; set; }
    public string details { get; set; }
    //public HttpPostedFileBase fileUpload { get; set; }
    //public string retrieveFile { get; set; }
    public  FileAttr files { get; set; }
}

public class FileAttr
{
    public HttpPostedFileBase fileUpload { get; set; }
    public string retrieveFile { get; set; }
}

var getDailyDevotions = db.DailyDevotions.Select(d => new { title = d.DevotionsTitle, details = d.DevotionsDetails, retriveFileAudio = d.VoiceNotes });
List<kingdomAddModel> listdevotions = new List<kingdomAddModel>();
foreach (var getDevotions in getDailyDevotions)
{
    kingdomlist = new kingdomAddModel();
    kingdomlist.title = getDevotions.title;
    kingdomlist.details = getDevotions.details;
    fileattr = new FileAttr();
    fileattr.retrieveFile = getDevotions.retriveFileAudio;
    kingdomlist.files.retrieveFile = fileattr.retrieveFile; //erros appears here!
}

The line line kingdomlist.files.retrieveFile throws the exception, tried googling but I dont get simular problem. I just want to assign the value and will pull on my view.

cramopy
  • 3,375
  • 5
  • 24
  • 42
katleho
  • 101
  • 11
  • Where are you initializing `kingdomlist.files`? – NASSER Sep 05 '15 at 16:46
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – dotnetom Sep 05 '15 at 16:50

1 Answers1

1

Do not access properties of FileAttr directly, only use files with the instance of kingdomAddModel. Don't mixup them

Replace

foreach (var getDevotions in getDailyDevotions)
{
    kingdomlist = new kingdomAddModel();
    kingdomlist.title = getDevotions.title;
    kingdomlist.details = getDevotions.details;
    fileattr = new FileAttr();
    fileattr.retrieveFile = getDevotions.retriveFileAudio;
    kingdomlist.files.retrieveFile = fileattr.retrieveFile; //erros appears here!
}

with

foreach (var getDevotions in getDailyDevotions)
{
    kingdomlist = new kingdomAddModel
    {
        title = getDevotions.title,
        details = getDevotions.details,
        files = new FileAttr
        {
            retrieveFile = getDevotions.retriveFileAudio,
            //fileUpload = some value here
        }
    };
    listdevotions.Add(kingdomlist);
}

OR use Linq

listdevotions = (from getDevotions in getDailyDevotions
                select new kingdomAddModel                
                {
                    title = getDevotions.title,
                    details = getDevotions.details,
                    files = new FileAttr
                    {
                        retrieveFile = getDevotions.retriveFileAudio,
                        //fileUpload = some value here
                    }
                }).ToList();
NASSER
  • 5,610
  • 7
  • 32
  • 54