2

I am having this strange null reference exception in razor view, here is my view model code

    public class RoomWantedAd
    {
        private List<ItemResource> _ItemResources = null;
        public RoomWantedAd()
        {
            _ItemResources = new List<ItemResource>();
        }
        public int RoomWantedId { get; set; }
        public Item Item { get; set; }
        public ItemDescription ItemDescription { get; set; }
        public ItemWanted ItemWanted { get; set; }
        public ItemLocation ItemLocation { get; set; }
        public ItemAddress ItemAddress { get; set; }
        public string CityName { get; set; }
        public string AreaName { get; set; }
        public List<ItemResource> ItemResources
        {
            get { return _ItemResources; }
            set { _ItemResources = value; }
        }
        public string PlaceId { get; set; }

        public string strCanMoveInFrom { get; set; }
    }

and my database model ItemWanted.cs code here

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    [Table("ItemWanted")]
    public partial class ItemWanted
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public ItemWanted()
        {
        }
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ItemWantedId { get; set; }

        [ForeignKey("Item")]
        public int ItemId { get; set; }
        public int? Budget { get; set; }

        [StringLength(10)]
        public string BudgetTimeSpan { get; set; }

        public DateTime? CanMoveInFrom { get; set; }

        [StringLength(3)]
        public string DaysToStay { get; set; }

        public bool? ParkingAmenity { get; set; }

        public bool? BalconyAmenity { get; set; }

        public bool? GardenAmenity { get; set; }

        public bool? IsAmenityEnabled { get; set; }

        public bool? GarageAmenity { get; set; }

        public bool? BroadBandAmenity { get; set; }

        public bool? FurnishedAmenity { get; set; }

        [StringLength(15)]
        public string YourOccupation { get; set; }

        [StringLength(3)]
        public string Age1 { get; set; }

        [StringLength(3)]
        public string Age2 { get; set; }

        public bool? Smoker { get; set; }

        public bool? Pets { get; set; }

        [StringLength(20)]
        public string Language { get; set; }

        [StringLength(20)]
        public string Nationality { get; set; }

        [StringLength(20)]
        public string Interests { get; set; }

        [StringLength(20)]
        public string DisplayName { get; set; }

        [StringLength(15)]
        public string FlatMateGender { get; set; }

        public bool? FlatMateSmoking { get; set; }

        [StringLength(3)]
        public string FlatMateAge1 { get; set; }

        [StringLength(3)]
        public string FlatMateAge2 { get; set; }

        public bool? FlatMatePets { get; set; }

        [StringLength(15)]
        public string FlatMateOccupation { get; set; }

        public int? NumberOfViews { get; set; }

        [StringLength(15)]
        public string YourGender { get; set; }

        public virtual Item Item { get; set; }
    }

and my razor view code is here

    </div>
<div class="row">
    <div class="col-md-9 text-left">
        <span>&pound</span> @Html.Label(RoomFlogContents.Classes.SearchUtility.GetPriceLabelItemWanted(item))
        <br />
        @Html.Label(item.ItemWanted.Item.ItemAddress.FormattedAddress, new { @class = "h4 inline-block" })
        @{
            DateTime dt = DateTime.Now;
            if (item.ItemWanted.CanMoveInFrom != null)
            {
                dt = (DateTime)item.ItemWanted.CanMoveInFrom;
            }

        }
            <br />
            <label>Can move in from:</label>
            @Html.Label(dt.ToString("dd/MM/yyyy"), new { @class = "h4 inline-block" })
    </div>
</div>

all the code runs fine, but when execution comes on my razor view code, at line

@Html.Label(item.ItemWanted.Item.ItemAddress.FormattedAddress, new { @class = "h4 inline-block" })

and

@Html.Label(dt.ToString("dd/MM/yyyy"), new { @class = "h4 inline-block" })

my code crashes and gives a null reference exception, i have checked through breakpoints whether or not the properties are null, but every instance property is instantiated properly and contains value.

I also used null checks but still it throws exception. Please somebody help me out.

ekad
  • 14,436
  • 26
  • 44
  • 46
Numan Ijaz
  • 878
  • 1
  • 8
  • 18
  • What happens if you use `.Value` when calling your nullable types? Like `dt = (DateTime)item.ItemWanted.CanMoveInFrom.Value` – Jacob Roberts May 29 '15 at 14:20
  • Where does `item` in the markup come from? Also note that in `Html.Label(dt.ToString...` dt is out of scope variable, i.e. it does not exist (assuming you pasted the code as is) – Andrei May 29 '15 at 14:42
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 29 '15 at 16:52

0 Answers0