2
Vote

NullRefenceException in ExcelRangeBase constructor

description

Opening the attached xlsx file generates a NullReferenceException in ExcelRangeBase constructor. The problem comes down to ExcelWorkbook.GetDefinedNames() line 171 has this code:
namedRange = _names.Add(elem.GetAttribute("name"), new ExcelRangeBase(Worksheets[addr._ws], fullAddress));
however, WOrksheets[addr._ws] returns null which later leads to ExcelRangeBase. At the time it's iterating over the defined name "RaceName" which has an InnerText of "#Ref!" and is not a valid worksheet.

I'm not sure why the xlsx has "#Ref!" inside it. Viewing it in Excel 2010 works fine without any errors visible anywhere.

Even if there was a formula error somewhere causing "#Ref!" somewhere, it shouldn't fail to open and certainly shouldn't throw a generic NullReferenceException.

My calling code to generate the exception is this:
        using (var xp = new ExcelPackage(new FileInfo(path)))
        {
            if (xp.Workbook.Worksheets.Count == 0)
the exception is thrown when I try to access Workbook property.

Thanks,

Sam

file attachments

comments

JanKallman wrote Jan 21, 2011 at 6:59 PM

Yes, defined names need some work. I'll see if I can add a fix to the next version