C# casting becomes null

So i’m turning off hinting on some of my components cause it greatly increases my computation time, but after using casting, i’m running into a problem where the cast just becomes null.

can someone please enlighten me on why this is?

Could it be that this raises an exception? Probably a item not found exception?

My guess would be that you have a problem due to the way you iterate over the datatree. No idea how your datatree is structured, but my guess is that the pth = new GH_Path(i) is the problem and it should rather be GH_Path pth = x.Paths[i] ? And then retrieve the Branch as x.Branch(pth) and then iterate over this list with j?

If you think the casting is the problem, just do it in a separate line, assign it to variable and test for nullity? Or if you can’t do that because the type doesn’t allow null as value, cast and catch the NullException

Thanks for your reply.

This is the data structure of the input

So for some reason the way i’m casting is creating null’s, so instead of (int) I have to use Convert.ToInt32() , I don’t know why, but it’s now working.

I also tried to reproduce this, but it works fine for me. Maybe try iterating over the datatree in a safe way ? Your code is prone to raise exceptions if something changes in the structure of the DataTree:

foreach (GH_Path path in x.Paths)
{
  List<System.Object> branch = x.Branch(path);
  foreach (object item in branch)
  {
    int i = (int) item;
    castedIntegers.Add(i, path);
  }
}