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>£</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.