troubleshooting Question

Mvc: How to handle null values

Avatar of Sirdots
Sirdots asked on
C#ASP.NET
4 Comments1 Solution482 ViewsLast Modified:
I am reading the content of an excel file and adding it to a collection.

[HttpPost]
        public ActionResult Index(HttpPostedFileBase file)
        {
            const int startRow = 1;

            if (file != null && Path.GetExtension(file.FileName) == ".xlsx")
            {
                IList<PersonalData> exampleDataList = new List<PersonalData>();
                using(var excel = new ExcelPackage(file.InputStream))
                {
                    //open and read the xlsx file.                  
                        //Get the work book in the file
                    ExcelWorkbook workBook = excel.Workbook;
                        if (workBook != null)
                        {
                            if (workBook.Worksheets.Count > 0)
                            {
                                //Get the first worksheet
                                ExcelWorksheet currentWorkSheet = workBook.Worksheets.First();
                 

                                for (int rowNumber = startRow + 1; rowNumber <= currentWorkSheet.Dimension.End.Row; rowNumber++)
                                // read each row from the start of the data (start row + 1 header row) to the end of the spreadsheet.
                                {
                                    object col1Value = currentWorkSheet.Cells[rowNumber, 1].Value;
                                    object col2Value = currentWorkSheet.Cells[rowNumber, 2].Value;
                                    object col3Value = currentWorkSheet.Cells[rowNumber, 3].Value;
                                    object col4Value = currentWorkSheet.Cells[rowNumber, 4].Value;

                                    if ((col1Value != null) && (col2Value != null)) && (col3Value != null)) && (col4Value != null))
                                    {
                                        exampleDataList.Add(new PersonalData {firstname = col1Value.ToString(),lastname = col2Value.ToString(), address=col3Value.ToString(), salary=col4Value.ToString() });
                                    }
                                 
                                }

                                int myresultcount = WriteToDb(exampleDataList);
                            }

                        }                  
                   
                }
            }
                     
            return RedirectToAction("Index");        
        }
            

public class PersonalData
    {
        public int Id { get; set; }
        public string  firstname { get; set; }
        public string lastname { get; set; }
        public string address { get; set; }
        public string salary { get; set; }
    }


Here is the error message I am getting:  object reference not set to an instance of an object


It blows up on this line.

exampleDataList.Add(new PersonalData {firstname = col1Value.ToString(),lastname = col2Value.ToString(), address=col3Value.ToString(), salary=col4Value.ToString() });

Am I doing something wrong here?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros