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

Free Support Forum - aspose.com

WorksheetCollection.GetNamedRanges() can return null

I think API wise it’s a dangerous choice, time to time we hit NullReferenceException when use GetNamedRanges(). It’s better to return an empty array instead of null.

Hi Dmitry,

Thank you for contacting Aspose support.

Please note, if a spreadsheet does not contain any defined named ranges, the WorksheetCollection.GetNamedRanges method returns (and should return) null. If you get NullReferenceException while accessing the named ranges from a particular spreadsheet, it could have been caused due to some bug in the API, and therefore needs investigation. Please share one such sample so we could raise the problem in our bug tracking system for thorough analysis.

Thank you for response, Babar. I think you misunderstood my previous post. I don’t observe any bugs in the implementation of this particular API. As as far as I see returning null is a design choice (confirmed in your response). My point is that this is a very poor design and leads to bugs with NullReferenceException in user’s code. I personally hit it couple of times. When API is used it’s usually tested by a dev with a valid case when there are named ranges, and a bug for the case without named ranges sometimes escapes testing.

A sensible fix can be to return an empty array instead of null (which can be globally cached for perf), or obsolete this method in favor a new method that always returns IEnumerable.

Hi Dmitry,

Thank you for the explanation. I can understand your concerns now. Please spare me some time to discuss this matter with the product team and get back with their point of view on this topic.

Hi again,

This is to update you that I have discussed your concerns with the product team. First of all, please note that the behaviour of WorksheetCollection.GetNamedRanges method is as per design. There are several other APIs in Aspose.Cells library that return null in case denoted objects are not available/found. If we change the behaviour of aforementioned method, the APIs will not be uniform anymore. Moreover, creating and returning an empty collection will definitely cause a bit performance lag as compared to returning null directly, even if the cost is very small. The best way to deal with such situations is to first check the returned value for null. In case it is not null, only then you should access the items from the collection.

Thank you for your understanding.