Quantcast
Channel: EPPlus Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 2262

Created Unassigned: System.NullReferenceException in ExcelRangeBase constructor [15256]

$
0
0
Hi,

I got the following exception when trying to read a XLSX file:

```
System.NullReferenceException: Object reference not set to an instance of an object
at OfficeOpenXml.ExcelRangeBase..ctor (OfficeOpenXml.ExcelWorksheet xlWorksheet, System.String address) [0x00000] in :0
at OfficeOpenXml.ExcelNamedRange..ctor (System.String name, OfficeOpenXml.ExcelWorksheet nameSheet, OfficeOpenXml.ExcelWorksheet sheet, System.String address, Int32 index) [0x00000] in :0
at OfficeOpenXml.ExcelNamedRangeCollection.Add (System.String Name, OfficeOpenXml.ExcelRangeBase Range) [0x00000] in :0
at OfficeOpenXml.ExcelWorkbook.GetDefinedNames () [0x00000] in :0
at OfficeOpenXml.ExcelPackage.get_Workbook () [0x00000] in :0
```

I think it is related to [this issue](https://epplus.codeplex.com/workitem/15038), and the reason is the same, "_worksheet" ExcelWorksheet member variable is null. However, the issue is marked as resolved.

I could fix this issue by adding conditional validators to check if the ExcelWorksheet parameters is null in the ExcelRangeBase constructors:

``` C#
#region Constructors
internal ExcelRangeBase(ExcelWorksheet xlWorksheet)
{
_worksheet = xlWorksheet;
if (xlWorksheet != null)
{
_ws = _worksheet.Name;
_workbook = _worksheet.Workbook;
}
else
{
_ws = null;
_workbook = null;
}
this.AddressChange += new EventHandler(ExcelRangeBase_AddressChange);
SetDelegate();
}

void ExcelRangeBase_AddressChange(object sender, EventArgs e)
{
if (Table != null)
{
SetRCFromTable(_workbook._package, null);
}
SetDelegate();
}
internal ExcelRangeBase(ExcelWorksheet xlWorksheet, string address) :
base(xlWorksheet == null ? "" : xlWorksheet.Name, address)
{
_worksheet = xlWorksheet;
if (xlWorksheet != null)
{
_workbook = _worksheet.Workbook;
base.SetRCFromTable(_worksheet._package, null);
}
else
{
_workbook = null;
base.SetRCFromTable(null, null);
}
if (string.IsNullOrEmpty(_ws)) _ws = _worksheet == null ? "" : _worksheet.Name;
this.AddressChange += new EventHandler(ExcelRangeBase_AddressChange);
SetDelegate();
}
internal ExcelRangeBase(ExcelWorkbook wb, ExcelWorksheet xlWorksheet, string address, bool isName) :
base(xlWorksheet == null ? "" : xlWorksheet.Name, address, isName)
{
SetRCFromTable(wb._package, null);
_worksheet = xlWorksheet;
_workbook = wb;
if (string.IsNullOrEmpty(_ws)) _ws = (xlWorksheet == null ? null : xlWorksheet.Name);
this.AddressChange += new EventHandler(ExcelRangeBase_AddressChange);
SetDelegate();
}
~ExcelRangeBase()
{
this.AddressChange -= new EventHandler(ExcelRangeBase_AddressChange);
}
#endregion
```

I didn't make a commit because I'm still not comfortable with Codeplex.

Thanks in advance!

Viewing all articles
Browse latest Browse all 2262

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>