[GIS] Replacing NULL value with Zero in geodatabase table using Python parser of ArcGIS field calculator

arcgis-10.1arcgis-desktoperror-999999field-calculatorpython-parser

I am having some difficulties to change "NULL" values to zero in geodatabase. I have tried the following python code in ArcGIS 10.1 ("Field Calculator") to change it but did not work.

Pre-Logic Script Code:

def RemoveNULL(x):
    if x is None:
        return ''
    elif x == '':
        return '0'
    else: return x

Calculation field:

RemoveNULL(str( !DEAD_VOL_PER_HA_SPP1_125!))

Error message:

ERROR: 999999: Error executing function
The value type is incompatible with the field type. [dead_pine]
Failed to execute (calculate field)

Best Answer

I can see from your field name !DEAD_VOL_PER_HA_SPP1_125! that the field you are calculating is likely a float or integer type field. Assuming this is true, you cannot write a space or '' into a number-based field (e.g. return ''). Valid values for number-based fields include None or a numeric value.

You will need to either create a new text field to store the special character (i.e. a space) or change the return output to an integer or float.

The following works for both text and numeric fields (Python Parser):

def replaceNull(x):
  if x is None:
    return 0
  else:
    return x

replaceNull(!YourFieldName!)
Related Question