Why am I getting AttributeError: 'NoneType' object has no attribute 'Name'

I’m using sensor conveyor to detect what kind of parts are going trough the conveyor and then I want to append certain types of parts to a list. The entire script is pretty long as there is lot of other things happening in the script but in short this is how I try to get the name:
sensor = app.findComponent(“Sensor Conveyor”)
sensor_signal = sensor.findBehaviour(“SensorSignal”)

and then later I try to read the name into a variable

part = sensor_signal.Value
and then I use part.Name when I want to know the name of the part but for some reason it is not working even though I have used this exactly same way before in different simulation and it worked perfectly then.
When I do “print part” and “print part.Name” it prints out <vcComponent object at 0x00000214CA278168> and the correct part name but then it also prints None and gives the error AttributeError: ‘NoneType’ object has no attribute ‘Name’. So what is going on? It prints out the correct name even though it complains it doesn’t exist? For a time it was also appending correct names to a list even though it was giving the same error and then suddenly it just stopped working. I have no idea what is causing this.

Terve @ceppo

You need to check that the sensor is not returning None. The sensor returns None if the part has already left the sensor region.

Try adding this to your code:

if sensor_signal.Value is not None:
    part = sensor_signal.Value
    print part, part.Name
    print 'Error, no part detected'

The sensor seems to return both name and none almost at the same time which is kinda weird since it should just read the sensors value once when it changes state. However if I just put pass into the else statement then it actually works just as I want it to work and no error message is given so problem solved. Thank you.