Weird error "object reference not set as object instance"

Hi, my friends,

This is apparently a “no brainer”, but I’ve lost a few hours already with this error and I’m left scratching my head, especially because I get 2 different results when running the code inside a C# component and after compiling in VS.
The component is part of a much larger script and it subdivides a region delimited by a number of side-by-side curves.

Well, I get the “object reference not set as object instance” error only on the compiled component (using exactly the same code inside the script).

Any clues?! Could it be that in the script the code is creating some null entries?

Turns out loop.IsValid is not enough to invalidate zero-length curves!!

One has to explicitly tell && loop.GetLength() > 0

The geometry generated within the script could sometimes produce zero-length curves (like when curves started/ended at the same point), circumstances which I was not recreating with the C# component.
Always thought .IsValid took care of all that mess.

Living and learning…