GetAsByteArray throwing NullReferenceException

Oct 11, 2012 at 7:55 AM

I have an existing spreadsheet that I open and edit and then save using GetAsByteArray.

Something in the last couple of EPPlus updates has caused it to stop working, sorry to say I don't know exactly which changeset caused it. I tried downloading both stable 3.1.1 and latest f23162df86ce but the error exists in both.

Any help is appreciated.

Best regards,

Andreas

 

This is the stack trace:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.


Source Error:

Line 188:    private static eExcelConditionalFormattingRuleType GetIconSetType(XmlNode topNode, XmlNamespaceManager nameSpaceManager)
Line 189:    {
Line 190:        var v = topNode.SelectSingleNode("d:iconSet/@iconSet", nameSpaceManager).Value;
Line 191:
Line 192:        if (v[0] == '3')


Source File: C:\svnwc\EPPlus f23162df86ce\EPPlus\ConditionalFormatting\ExcelConditionalFormattingRuleType.cs    Line: 190

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   OfficeOpenXml.ConditionalFormatting.ExcelConditionalFormattingRuleType.GetIconSetType(XmlNode topNode, XmlNamespaceManager nameSpaceManager) in C:\svnwc\EPPlus f23162df86ce\EPPlus\ConditionalFormatting\ExcelConditionalFormattingRuleType.cs:190
   OfficeOpenXml.ConditionalFormatting.ExcelConditionalFormattingRuleType.GetTypeByAttrbiute(String attribute, XmlNode topNode, XmlNamespaceManager nameSpaceManager) in C:\svnwc\EPPlus f23162df86ce\EPPlus\ConditionalFormatting\ExcelConditionalFormattingRuleType.cs:136
   OfficeOpenXml.ConditionalFormatting.ExcelConditionalFormattingCollection..ctor(ExcelWorksheet worksheet) in C:\svnwc\EPPlus f23162df86ce\EPPlus\ConditionalFormatting\ExcelConditionalFormattingCollection.cs:153
   OfficeOpenXml.ExcelWorksheet.get_ConditionalFormatting() in C:\svnwc\EPPlus f23162df86ce\EPPlus\ExcelWorksheet.cs:2723
   OfficeOpenXml.ExcelStyles.UpdateXml() in C:\svnwc\EPPlus f23162df86ce\EPPlus\ExcelStyles.cs:608
   OfficeOpenXml.ExcelWorkbook.Save() in C:\svnwc\EPPlus f23162df86ce\EPPlus\ExcelWorkbook.cs:649
   OfficeOpenXml.ExcelPackage.GetAsByteArray(Boolean save) in C:\svnwc\EPPlus f23162df86ce\EPPlus\ExcelPackage.cs:935
   OfficeOpenXml.ExcelPackage.GetAsByteArray() in C:\svnwc\EPPlus f23162df86ce\EPPlus\ExcelPackage.cs:905
   HartleyAndMarks.Services.Controllers.OrderFormController.FileExcel(String filename, String sheetname, String country, Nullable`1 lastActivity, Nullable`1 useStoreLocator) in C:\svnwc\HartleyAndMarks.Services\HartleyAndMarks.Services\Controllers\OrderFormController.cs:223
   lambda_method(Closure , ControllerBase , Object[] ) +378
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +263
   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
   System.Web.Mvc.Controller.ExecuteCore() +116
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8967885
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

 

Oct 11, 2012 at 10:11 AM

I did a quick fix that seems to work. But my knowledge of both EPPlus source and xml is very limited so it might be a very bad solution.)

private static eExcelConditionalFormattingRuleType GetIconSetType(XmlNode topNode, XmlNamespaceManager nameSpaceManager)
{
	//var v = topNode.SelectSingleNode("d:iconSet/@iconSet", nameSpaceManager).Value;

	//if (v[0] == '3')
	if (topNode.SelectSingleNode("d:iconSet/d:cfvo[position()=3]", nameSpaceManager) != null)
	{
		return eExcelConditionalFormattingRuleType.ThreeIconSet;
	}
	//else if (v[0] == '4')
	else if (topNode.SelectSingleNode("d:iconSet/d:cfvo[position()=4]", nameSpaceManager) != null)
	{
		return eExcelConditionalFormattingRuleType.FourIconSet;
	}
	else
	{
	  return eExcelConditionalFormattingRuleType.FiveIconSet;
	}
}