Click here to Skip to main content
15,907,149 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi i got Object reference not set to an instance of an object.

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

In this above line Near to Viewmodel.TaxInfoTaxFeld I got this Error. Please any one Correct my code.

My Model
C#
public partial class TaxInfoTaxFiled
{
    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; }
}
public partial class TaxField
{
    public System.Guid TaxFieldID { get; set; }
    public string DisplayName { get; set; }
    public string PrintName { get; set; }
}


My ViewModel
C#
 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> TaxField { get; set; }
    public List<TaxInfoTaxFiled> TaxInfoTaxFiled { get; set; }
}


My Controller

C#
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.TaxInfoTaxFiled)
 {
      foreach (var i in Alist)
      {
          tax.FieldTypeID = Guid.Parse(i.ToString());
      }
      foreach (var j in objValue)
      {
          tax.FieldValue = j.ToString();       
      }
      db.TaxInfoTaxFiled.Add(tax);
  }



My View

<div class="editor-label">
       @Html.LabelFor(model => model.TinNo)
   </div>
   <div class="editor-field">
       @Html.EditorFor(model => model.TinNo)
       @Html.ValidationMessageFor(model => model.TinNo)
   </div>

   <div class="editor-label">
       @Html.LabelFor(model => model.CstNo)
   </div>
   <div class="editor-field">
       @Html.EditorFor(model => model.CstNo)
       @Html.ValidationMessageFor(model => model.CstNo)
   </div>

   <div class="editor-label">
       @Html.LabelFor(model => model.ExciseRegNo)
   </d
   <div class="editor-label">
       @Html.LabelFor(model => model.PanNo)
   </div>
   <div class="editor-field">
       @Html.EditorFor(model => model.PanNo)
       @Html.ValidationMessageFor(model => model.PanNo)
   </div>

   <div class="editor-label">
       @Html.LabelFor(model => model.ServiceTaxNo)
   </div>
   <div class="editor-field">
       @Html.EditorFor(model => model.ServiceTaxNo)
       @Html.ValidationMessageFor(model => model.ServiceTaxNo)
   </div>

   <div class="editor-label">
       @Html.LabelFor(model => model.CinNo)
   </div>
   <div class="editor-field">
       @Html.EditorFor(model => model.CinNo)
       @Html.ValidationMessageFor(model => model.CinNo)
   </div>

   <p>
       <input type="submit" value="Create" />
   </p>
Posted
Comments
F-ES Sitecore 7-Dec-15 6:23am    
viewmodel is null or viewmodel.TaxInfoTaxFiled is null. You haven't posted the relevant code though.

http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it
Member 12087373 7-Dec-15 6:29am    
yes its showing null the object is not refered by the view model to controller to refer the object from viewmodel to Controller what shall i do?
F-ES Sitecore 7-Dec-15 6:32am    
You haven't posted the relevant code, it's impossible for us to know. You will have to have created an instance of viewmodel somewhere. Null reference exceptions can usually only be fixed by you as we can't run your code. Go through the advice on the link which will give you ideas on how to track the issue down yourself.

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 
Comments
Member 12087373 7-Dec-15 6:56am    
I posted my code too I got error in foreach (var tax in viewmodel.TaxInfoTaxFileds)
{}
I kept debug point in For each loop In this line only I got that error . Especially On TaxInfoTaxField.

Actually I created the list in Viewmodel

public List<taxfield> TaxFields { get; set; }
public List<taxinfotaxfiled> TaxInfoTaxFileds { get; set; } and refer the obj TaxInfoTaxFields obj to foreach loop of controller
OriginalGriff 7-Dec-15 7:00am    
So you need to look at why it's null: either your viewmodel is null, or the TaxInfoTaxFields is - the debugger will tell you which.
Then you can look back in your code to see why - and that will likely depend on what actions your user has performed before...
foreach (var i in Alist)
{
tax.FieldTypeID = Guid.Parse(i.ToString());
}
Here is chance to get i value as empty or null value. so conversion problem
 
Share this answer
 
v4
Comments
Member 12087373 7-Dec-15 7:05am    
Actullu I created Viewmodel to assign the value to Viewmodel. If the value has been enter in the Tinno, CstNo, ServiceTaxNo, Excise RegNo, CinNo, Panno all these 6 fields values of Customer Form are save in Fieldvalue of TaxinfoTaxfield table . So these 6 six fields are same save in same column but next next row so i created list and for loop in Viewmodel and Controller

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


My ViewMOdel

public List<taxfield> TaxField { get; set; }
public List<taxinfotaxfiled> TaxInfoTaxFiled { get; set; }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900