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

Free Support Forum - aspose.com

Range SetStyle fails with NullReferenceException error

Hi

In this code part, I face NullReferenceException on last SetStyle call:

rng = ws.Cells.CreateRange(0, 0, fss.Count + 1, 5)
st = New Aspose.Cells.Style
st.Font.Name = “B Nazanin”
st.Font.Size = 11
rng.SetStyle(st)
rng = ws.Cells.CreateRange(fss.Count + 1, 0, 1, 7)
rng.Merge()
rng = ws.Cells.CreateRange(fss.Count + 1, 0, 1, 5)
st = New Aspose.Cells.Style
st.Font.Name = “B Nazanin”
st.Font.Size = 11
st.HorizontalAlignment = Cells.TextAlignmentType.Center
rng.SetStyle(st)
rng = ws.Cells.CreateRange(0, 6, fss.Count + 1, 5)
st = New Aspose.Cells.Style
st.Font.Name = “B Nazanin”
st.Font.Size = 11
st.HorizontalAlignment = Cells.TextAlignmentType.Center
rng.SetStyle(st)

fss.Count equals to 5. Both SetStyle calls prior to this line are successful. During debug, I can see that rng refers to G1:K6 range and st includes font given (B Nazanin, 11, regular). What’s wrong? Platform info: Aspose.Cells 8.6.0.0 on VS 2013, Server 2012 R2.

UPDATE:

I changed code lines’ order to this (as in my case, lines’ order SHOULD NOT matter):

rng = ws.Cells.CreateRange(fss.Count + 1, 0, 1, 7)
rng.Merge()
rng = ws.Cells.CreateRange(0, 6, fss.Count + 1, 5)
st = New Aspose.Cells.Style
st.Font.Name = “B Nazanin”
st.Font.Size = 11
st.HorizontalAlignment = Cells.TextAlignmentType.Center
rng.SetStyle(st)
rng = ws.Cells.CreateRange(fss.Count + 1, 0, 1, 5)
st = New Aspose.Cells.Style
st.Font.Name = “B Nazanin”
st.Font.Size = 11
st.HorizontalAlignment = Cells.TextAlignmentType.Center
rng.SetStyle(st)
rng = ws.Cells.CreateRange(0, 0, fss.Count + 1, 5)
st = New Aspose.Cells.Style
st.Font.Name = “B Nazanin”
st.Font.Size = 11
rng.SetStyle(st)

This caused the problem to be moved to second call! Why should this happen?!

Hi Mohamamd,


Thank you for contacting Aspose support.

I am afraid, we require more information to properly evaluate the presented scenario on our side. However, first we suggest you try the latest build of Aspose.Cells for .NET 8.6.0.4 in your project to see if you can still replicate the said problem. In case the problem persists, please provide us an executable sample application as we need your complete code for analysis. Moreover, if you are reading a pre-built spreadsheet to manipulate it with Aspose.Cells for .NET API then we will require it as well. Please note, some issues are template specific, that means, the issue occur while processing specific files therefore it is best to evaluate the case with your sample rather than creating our own.

Hi,


Please use Workbook.CreateStyle() instead to create or instantiate Style object as it is better way for creating/adding Style object, please change your original code to following, it would work fine:
e.g
Sample code:

Dim workbook as Workbook = New Workbook()
Dim ws as Worksheet = workbook.Worksheets(0)

rng = ws.Cells.CreateRange(0, 0, fss.Count + 1, 5)
st = workbook.CreateStyle()
st.Font.Name = “B Nazanin”
st.Font.Size = 11
rng.SetStyle(st)
rng = ws.Cells.CreateRange(fss.Count + 1, 0, 1, 7)
rng.Merge()
rng = ws.Cells.CreateRange(fss.Count + 1, 0, 1, 5)
st = workbook.CreateStyle()
st.Font.Name = “B Nazanin”
st.Font.Size = 11
st.HorizontalAlignment = Cells.TextAlignmentType.Center
rng.SetStyle(st)
rng = ws.Cells.CreateRange(0, 6, fss.Count + 1, 5)
st = workbook.CreateStyle()
st.Font.Name = “B Nazanin”
st.Font.Size = 11
st.HorizontalAlignment = Cells.TextAlignmentType.Center
rng.SetStyle(st)


Let us know if you still have any issue.

Thank you.

Thanks for your reply.

I am writing a large web application including a large database and so I can’t send any file from this project, but I can provide complete code on this ASP.NET/VB.NET page, if it helps in any way. I am not using a pre-built spreadsheet. This workbook is being created from scratch in a single-subroutine code. I will give newer version a try and tell if it solves my problem.

Regards

babar.raza:
Hi Mohamamd,

Thank you for contacting Aspose support.

I am afraid, we require more information to properly evaluate the presented scenario on our side. However, first we suggest you try the latest build of Aspose.Cells for .NET 8.6.0.4 in your project to see if you can still replicate the said problem. In case the problem persists, please provide us an executable sample application as we need your complete code for analysis. Moreover, if you are reading a pre-built spreadsheet to manipulate it with Aspose.Cells for .NET API then we will require it as well. Please note, some issues are template specific, that means, the issue occur while processing specific files therefore it is best to evaluate the case with your sample rather than creating our own.


This caused my application to completely go out of service! On first button click, nothing happens (no error message(s), no output, no message from application - which should display at least a success or error message). On second button click, an error on license loading line appears. Cleaning solution, changing reference back to previous DLL and any other attempt to go back to previous state fails.
Amjad Sahi:
Hi,

Please use Workbook.CreateStyle() instead to create or instantiate Style object as it is better way for creating/adding Style object, please change your original code to following, it would work fine:
e.g
Sample code:

Dim workbook as Workbook = New Workbook()
Dim ws as Worksheet = workbook.Worksheets(0)

rng = ws.Cells.CreateRange(0, 0, fss.Count + 1, 5)
st = workbook.CreateStyle()
st.Font.Name = "B Nazanin"
st.Font.Size = 11
rng.SetStyle(st)
rng = ws.Cells.CreateRange(fss.Count + 1, 0, 1, 7)
rng.Merge()
rng = ws.Cells.CreateRange(fss.Count + 1, 0, 1, 5)
st = workbook.CreateStyle()
st.Font.Name = "B Nazanin"
st.Font.Size = 11
st.HorizontalAlignment = Cells.TextAlignmentType.Center
rng.SetStyle(st)
rng = ws.Cells.CreateRange(0, 6, fss.Count + 1, 5)
st = workbook.CreateStyle()
st.Font.Name = "B Nazanin"
st.Font.Size = 11
st.HorizontalAlignment = Cells.TextAlignmentType.Center
rng.SetStyle(st)


Let us know if you still have any issue.

Thank you.


Thanks a LOT! It worked GREAT! Many many thanks!

Hi,

Thanks for your feedback and using Aspose.Cells.

It is good to know that the given code was helpful in resolving this issue. Let us know if you encounter any other issue, we will be glad to look into it and help you further.