Model Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace testing.Models
{
public class SchemaDefinition
{
public List<RecordInfo> Input { get; set; }
public List<RecordInfo> Output { get; set; }
}
public class RecordInfo
{
public string RecordId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<FieldInfo> FieldInfo { get; set; }
}
public class FieldInfo
{
public string Name { get; set; }
public string Description { get; set; }
public int Length { get; set; }
public int Start { get; set; }
public int End { get; set; }
public string Format { get; set; }
}
}
Controller Code
var SchemaDefintion = new SchemaDefinition();
for (int Currentsheet = 1; Currentsheet <= WorksheetCount; Currentsheet=Currentsheet+1)
{
Excel.Worksheet worksheet = workbook.Sheets[Currentsheet];
string SheetName = worksheet.Name;
if (SheetName.Substring(0, 2) != "A" || SheetName.Substring(0, 2) != "B")
{
//Error that invalid excel sheet
}
Excel.Range range = worksheet.UsedRange;
var ListFields = new List<FieldInfo>();
for (int row = 2; row <= range.Rows.Count; row++)
{
var Fieldproperties = new FieldInfo {Name = ((Excel.Range)range.Cells[row, 1]).Text, Description = ((Excel.Range)range.Cells[row, 2]).Text,
Length =Convert.ToInt32(((Excel.Range)range.Cells[row, 3]).Text),
Start=Convert.ToInt32( ((Excel.Range)range.Cells[row, 4]).Text),
End=Convert.ToInt32( ((Excel.Range)range.Cells[row, 5]).Text),
Format= ((Excel.Range)range.Cells[row, 6]).Text
};
ListFields.Add(Fieldproperties);
}
SchemaDefintion.Input.Add(new testing.Models.RecordInfo { RecordId = "A", Name = "header", Description = "description", FieldInfo = ListFields });
When i put debug in my code it's throws an error at the line below
SchemaDefintion.Input.Add(new testing.Models.RecordInfo { RecordId = "A", Name = "header", Description = "description", FieldInfo = ListFields });`
The Error descriptions as
Server Error in '/' Application.
________________________________________ Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.