This project is read-only.

Object reference not set to an instance of an object-error?

Dec 8, 2015 at 5:51 AM
Hi i got Object reference not set to an instance of an object.

Error Line : foreach (var tax in viewmodel.Tfs)

In this above line Near to Viewmodel.Tfs I got this Error exactly on that __(Tfs) __object. it showing Null .Please any one Correct my code.

My Model

public partial class TaxFieldModel
{
public System.Guid TaxFieldID { get; set; }
public string DisplayName { get; set; }
public string PrintName { get; set; }
}
public partial class TaxInfoTaxFiledModel
{
public System.Guid TaxInfoTaxFieldID { get; set; }
public Nullable<System.Guid> TaxInfoID { get; set; }
public Nullable<System.Guid> TaxFieldID { get; set; }
public Nullable<System.Guid> FieldTypeID { get; set; }
public string FieldValue { get; set; }
}

My ViewModel

public class CustomerViewModel
{
public string TinNo { get; set; }
public string CstNo { get; set; }
public string ExciseRegNo { get; set; }
public string PanNo { get; set; }
public string ServiceTaxNo { get; set; }
public string CinNo { get; set; }


public List<TaxField> TaxFields { get; set; }
public List<TaxInfoTaxFiled> Tfs{ get; set; }

}

public class VisitorsEntities1 : DbContext
{
public DbSet<Sample_Customer.Models.CustomerModel.TaxFieldModel> TaxField { get; set; }
public DbSet<Sample_Customer.Models.CustomerModel.TaxInfoTaxFiledModel>TaxInfoTaxFiled { get; set; }
}

My Controller

ArrayList Alist = new ArrayList();
{
Alist.Add("FD713788-B5AE-49FF-8B2C-F311B9CB0CC4");
Alist.Add("64B512E7-46AE-4989-A049-A446118099C4");
Alist.Add("376D45C8-659D-4ACE-B249-CFBF4F231915");
Alist.Add("59A2449A-C5C6-45B5-AA00-F535D83AD48B");
Alist.Add("03ADA903-D09A-4F53-8B67-7347A08EDAB1");
Alist.Add("2F405521-06A0-427C-B9A3-56B8931CFC57");
}
ArrayList objValue=new ArrayList();
{
objValue.Add(viewmodel.TinNo);
objValue.Add(viewmodel.CstNo);
objValue.Add(viewmodel.PanNo);
objValue.Add(viewmodel.CinNo);
objValue.Add(viewmodel.ExciseRegNo);
objValue.Add(viewmodel.ServiceTaxNo);
}

foreach (var tax in viewmodel.Tfs )
{
foreach (var i in Alist)
{
tax.FieldTypeID = Guid.Parse(i.ToString());
}
foreach (var j in objValue)
{
tax.FieldValue = j.ToString();
}
db.TaxInfoTaxFiled.Add(tax);
}

To save these Six Fields Values and ID's in TaxInfoTaxField Table in above Format I decided to use List. So I created List in ViewModel

public List<TaxField> TaxFields { get; set; }
public List<TaxInfoTaxFiled> Tfs{ get; set; }

And Refer these Object in Controller


foreach (var tax in viewmodel.Tfs )
{
}

But I got error "Object reference not set to an instance of an object-error?" in For each loop and exactly on Tfs.(object). That is (it is Null)The Controller can't get the Object Reference from Viewmodel to Controller. I tried Many method. But I donno How to clear this Error. please Correct my Code