'NoneType' object is not subscriptable

Hey there,
I am new to python in general. Hence I am struggling with the debugging in GhPython Component
I am trying to create a script that takes in 2 curves, a set maximum distance and finds a series of “best_lines” to connect them in the following way: The distance between the lines in no bigger then the max_distance, each line is the most perpendicular to the tangents of the evaluated points on the curve.
Now when I run the script once it’s doing what it’s supposed to but if I try and make a while loop to run it multiple times, each time trimming the curves and making them shorter, I face this: “Runtime error (TypeErrorException): ‘NoneType’ object is not subscriptable; Traceback:
line 52, in script”. And I don’t know how to approach the issue?
Perp_lines_between2crv.py (2.3 KB)

Usually that means you’re trying to run a method on some data you think should be there, but in fact somewhere before that line, something else has failed, and the data you think should be there is actually None. The way to find what failed is to place breakpoints above - where the data you need on line 52 being created - and look at the results.

Line 52 is

leftover1 = rs.coercecurve(rs.SplitCurve(curve1, param1, False)[1])

So in this case I think think rs.SplitCurve() is failing, in theory it should return a list from which you are trying to reference the item at index 1, but you are trying to get the index of a list that doesn’t exist - as SplitCurve() has returned None.

This is also a good example of why nested functions like the line above are not necessarily good practice - it’s maybe more compact looking, but you have no error checking if the inner function fails somehow. I would only nest functions where I am sure that the inner function cannot fail. Otherwise separate them and do error checking in between.

2 Likes

Thanks Helvetosaur for the fast reply. I added the extra coercecurve in despair, as I thought it’s failing to convert from a simple list item into a curve type. Even if I remove it gives the same error. so there is something with the rs.SplitCurve function, as even if I don’t do a while loop and just go “lumberjack” and connect the same script in itself for a second iteration it fails. And can’t figure out why, the Split.Curve(c,p, bool) [1] should give the second half of the curve after the split.