0

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.

Community
  • 1
  • 1
Jayendran
  • 9,638
  • 8
  • 60
  • 103
  • 3
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Tetsuya Yamamoto May 29 '17 at 08:27
  • 1
    Input and Output properties was not initialized, so, both are null when you try to add... – Tistkle May 29 '17 at 08:30
  • How can we initialize the input/output properties for my MVC code.I'm just new to this ! – Jayendran May 29 '17 at 08:33

1 Answers1

0

Thanks @Tistkle

I just initialize the Corresponding Input and output in my Controller code

SchemaDefintion.Input = new List<RecordInfo>();

SchemaDefintion.Output = new List<RecordInfo>();

Now it's working fine.

Jayendran
  • 9,638
  • 8
  • 60
  • 103