1 Replies - 933 Views - Last Post: 16 October 2014 - 01:10 PM Rate Topic: -----

#1 ibnmvungi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 20-April 12

Object reference not set to an instance of an object.

Posted 15 October 2014 - 12:29 AM

I have the following query


Controller;
  ViewData["AllYear"] = db.ATTENDANCEs.Select(a => new { a.ATTEND_YEAR.Year }).Distinct().OrderBy(a=>a.Year).ToList();




View:

  @foreach (var item in ViewData["AllYear"] as List<HR_Mvc.Models.ATTENDANCE>)
     {
         <li>
            <a href="/Admin/MoreReport/@item.ATTEND_YEAR.Year">@item.ATTEND_YEAR.Year</a>
         </li> 
                                      
     }




But I get this error "Object reference not set to an instance of an object."

The ViewData["AllYear"] seems not initialized.

Is This A Good Question/Topic? 0
  • +

Replies To: Object reference not set to an instance of an object.

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: Object reference not set to an instance of an object.

Posted 16 October 2014 - 01:10 PM

if any of the ATTEND_YEAR fields are null then calling .year on it would cause a null reference error. If you are sure they all have values then try debugging and make sure that your ViewData object is getting the data
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1