0

i put a break point at the beginning of import method and the code always enter the first condition even if i import an excel file and this cause the exception, the exception say that An exception of type 'System.NullReferenceException' occurred in TestImportFromExcel.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.

  public ActionResult Import(HttpPostedFileBase excelFile)
    {

            if (excelFile.ContentLength == 0 || excelFile == null)
            {
                ViewBag.Error = "select excel file <br/>";
                return View("Index");
            }

            else
            {
                //if file is not null
                if (excelFile.FileName.EndsWith("xls") || 
                  excelFile.FileName.EndsWith("xlsx"))
                {
                    string path = Server.MapPath("~/Content" + 
                       excelFile.FileName);
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.File.Delete(path);

                    }
                    excelFile.SaveAs(path);
                    Excel.Application application = new Excel.Application();

                    Excel.Workbook workBook = 
                    application.Workbooks.Open(path);
                    Excel.Worksheet worksheet = workBook.ActiveSheet;
                    Excel.Range range = worksheet.UsedRange;
                    List<ItemDetails> itemDetails = new List<ItemDetails>();
                    for (int x = 1; x < range.Rows.Count; x++)
                    {
                        ItemDetails i = new ItemDetails();
                        i.Id = ((Excel.Range)range.Cells[x, 1]).Text;
                        i.Factory = ((Excel.Range)range.Cells[x, 2]).Text;
                        i.ItemCode = ((Excel.Range)range.Cells[x, 3]).Text;
                        i.Description = ((Excel.Range)range.Cells[x,4]).Text;
                        i.UnitMeasure = ((Excel.Range)range.Cells[x,5]).Text;
                        i.Weight = ((Excel.Range)range.Cells[x, 6]).Text;
                        itemDetails.Add(i);
                    }
                    ViewBag.itemDetails = itemDetails;
                    return View("Success");
                }
                else
                {
                    ViewBag.Error = "the file type is not correct<br/>";
                    return View("Index");
                }
            }
        }
Asmaa Nour
  • 15
  • 5
  • In which line will the NullReferenceException occures? Where is your Breakpoint? What have you tried to find the error? Have you step-through with the debugger? – David Jul 16 '18 at 10:33
  • i put the Breakpoint at this line : if (excelFile.ContentLength == 0 || excelFile == null) to determine whether it will enter the condition or not, and it enters the if condition, so it can not read the excel file that i uploaded , it just sees the file is null – Asmaa Nour Jul 16 '18 at 11:59
  • First of all, change the order in your if to `(excelFile == null || excelFile.ContentLength == 0)` – Magnetron Jul 16 '18 at 12:03

1 Answers1

0
if (excelFile.ContentLength == 0 || excelFile == null)

needs to be

if (excelFile == null || excelFile.ContentLength == 0)

So that the ContentLength property is not evaluated if the first condition already is true.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • When i did that the exception went, but it still enters the first condition and can not read my excel file – Asmaa Nour Jul 16 '18 at 12:10
  • Can you describe (or better post an [MCVE]) what you do to *call* your method? The parameter is `null` your code now handles it correctly. We need more information to help you with the other problem. – nvoigt Jul 16 '18 at 12:14
  • in my index view i write this code: @using (Html.BeginForm("Import", "ItemDetails", FormMethod.Post, new { enctype="multipart/form-data"})) { @Html.Raw(ViewBag.Error); import from Excel : } so i uploaded an excel file and press on import button, the problem is that the code can not see the excel file, it sees that the excel file is null although i have been already uploaded it with extension xlsx. – Asmaa Nour Jul 16 '18 at 12:32
  • Please add this information to your question. As the question about the `null` value handling is closed, I'd suggest you open a new question, this time with the code you just posted in the comment and the bugfix we discussed here. – nvoigt Jul 16 '18 at 12:36
  • i have already done – Asmaa Nour Jul 16 '18 at 13:25