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

Free Support Forum - aspose.com

Error setting font color


I encountered a strange error when trying to set the font color of a Style object. Isolating the problem, I create a console app with only these lines of code in the Main:

Style style = new Style();
style.Font.Color = System.Drawing.Color.Blue;
Console.WriteLine("current value: {0}", style.Font.Color.ToString());

After assigning the Color object to the Color member of Font, the debugger tells me "'style.Font.Color' threw an exception of type 'System.NullReferenceException'". When trying to write the value, the debugger tells me, rather expectedly, that a "NullReferenceException" was unhandled. This is really weird, as Color is supposed to be a struct, which aren't nullable. Is there something fishy going on in the setter of Color? Is there anything I can do to circumvent this problem? I'm using Aspose.Cells.dll version

Best regards,

Kilian Hekhuis


Thanks for your query.

I think it might be due to restriction for Style object the way you are creating, i.e.:
Style style = new Style();
We might restrict instantiating the styles using new keyword for performance or other issues.

Please change your lines of code to:

Workbook wb = new Workbook();
Style style = wb.CreateStyle();
style.Font.Color = System.Drawing.Color.Blue;
Console.WriteLine(“current value: {0}”, style.Font.Color.ToString());

I have tested and it works fine and as expected.

Thank you.

Thanks very much for giving such an extremely fast and useful answer!