0

I've succeeded running such code in the pycharm. but I failed when I just copied the code to the code block of the field calculator. I am quite puzzled. What is wrong?

enter image description here

the code is:

def calVB(appeal,VB,ttmin):
    if(VB!=0):
        temp=appeal / (VB*ttmin)
    else:
        temp=0.1
    return temp

the expression is:

calVB( !facilities.appeal!, !ODMAX.VBFld!, !ODMAX.Total_Minu!)

arcpy code:

import arcpy
from arcpy import env
try:
    # set the environments
    env.workspace = "E:/Emily/code/result0610.gdb"
    env.qualifiedFieldNames = "UNQUALIFIED"

# Define script parameters    
    inFeatures = "E:/Emily/code/point/test/ODMAX.shp"
    layerName = "veg_layer"
    newField = "influence"
    joinTable = "E:/Emily/code/point/test/facilities.dbf"
    joinField1="OriginID"
    joinField2="OBJECTID"
    #calcExpression = "(!population._F_1! / !ODMAX.Total_Minu!)+1"
    calcExpression = "calVB( !facilities.appeal!, !ODMAX.VBFld!, !ODMAX.Total_Minu!)"
    codeblock="""def calVB(appeal,VB,ttmin):
        if(VB!=0):
            temp=appeal / (VB*ttmin)
        else:
            temp=0.1
        return temp"""
    outFeature = "E:/Emily/code/point/test/"+layerName+".shp"

# Add the new field
    arcpy.AddField_management (inFeatures, newField, "double",field_is_nullable="NULLABLE")

# Create a feature layer from the vegtype featureclass
    arcpy.MakeFeatureLayer_management (inFeatures,  layerName)

# Join the feature layer to a table
    arcpy.AddJoin_management (layerName, joinField1, joinTable, joinField2)
    for fld in arcpy.ListFields(layerName):
        print fld.name
# Populate the newly created field with values from the joined table
    arcpy.CalculateField_management (layerName, newField, "0", "PYTHON")
    print "0"
    arcpy.CalculateField_management (layerName, newField, calcExpression, "PYTHON",codeblock)
    print "cal done!"
# Remove the join
    arcpy.RemoveJoin_management (layerName)
    print
    print
    for fld in arcpy.ListFields(layerName):
        print fld.name

# Copy the layer to a new permanent feature class
    arcpy.CopyFeatures_management (layerName, outFeature)

except Exception, e:

    import traceback, sys
    tb = sys.exc_info()[2]
    print "Line %i" % tb.tb_lineno
    print e.message

ERROR: enter image description here

ERROR 999999: Error executing function.
The field is not nullable. [influence]
The field is not nullable. [influence]
Failed to execute (CalculateField).
2
  • The code creates the field with a setting that allows NULL values. When you manually create the field, are you making sure that field still allows NULL?
    – SMiller
    Commented Jun 30, 2018 at 5:59
  • 5
    Most likely joined table is not visible by one of them.
    – FelixIP
    Commented Jun 30, 2018 at 6:05

1 Answer 1

1

I had success on a simpler code in ArcGIS Pro by adding a catchall else statement at the end returning "invalid" as a result. Now it can't create a null value and the error is gone. This had the same error:

if ($feature.YrSinceBrn<$feature.Burn_Min)
return "In Rotation"
else if ($feature.YrSinceBrn<$feature.Burn_max)
return "Due"
else if ($feature.YrSinceBrn>$feature.Burn_max)
return "Overdue"

I added this:

else {}
return "Invalid"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.