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).
  • 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 Jun 30 '18 at 5:59
  • 4
    Most likely joined table is not visible by one of them. – FelixIP Jun 30 '18 at 6:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.