We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

NullReferenceException while calling SetBorder

Hi,


I noticed that if you do something like this:

style.SetBorder(BorderType.LeftBorder | BorderType.RightBorder, CellBorderType.Thick, Color.Black);

I get this:

System.NullReferenceException: Object reference not set to an instance of an object.
at Aspose.Cells.Style.SetBorder(BorderType borderEdge, CellBorderType borderStyle, Color borderColor)

Is this not supported? Will I have to call SetBorder for each BorderType I want to set instead?
I suggest that at the very least the exception thrown should be more informative.

Hi,


Yes, you got to set borders for each Border type. Please change the line of code:
i.e.,

style.SetBorder(BorderType.LeftBorder | BorderType.RightBorder, CellBorderType.Thick, Color.Black);

with:

style.SetBorder(BorderType.LeftBorder, CellBorderType.Thick, Color.Black);
style.SetBorder(BorderType.RightBorder, CellBorderType.Thick, Color.Black);


FYI, if you are setting the outline borders for a range, then you may adopt the single line approach:
e.g
Sample code:

range.SetOutlineBorder(BorderType.TopBorder | BorderType.LeftBorder | BorderType.RightBorder | BorderType.BottomBorder, CellBorderType.Thin, System.Drawing.Color.Black);

Thank you.



Thanks for your reply.


I must say that it is a bit counter-intuitive if one method allows the BorderType to be used as a Flag and the other does not.

The workaround works though so that’s ok.

Hi,


We will check the feasibility if Style.SetBorder() can be used the same way as Range.SetOutlineBorder() method, we may support it in future versions. Currently, you may adopt the approach as suggested.


Thank you.