Python Field Calculator Null Values

3648
4
02-15-2011 03:45 PM
JasonTipton
Occasional Contributor III
I am trying to calculate the value of a field based on multiple other fields. My code works fine in Python, but it bombs in ArcPad. The reason must be that I have Null values in a lot of my fields.

Here is a very simplified illustration:

def main (a,b):
  return a


field1 =
 main(1, !field2!)


where !field2! is a field that is Null

I receive no error, it just won't place the calculated "1" into field2. If you were to put:

field1 =
main(1,2)

or
field1 =
main(!field3!, !field4!)

with field3 and field4 holding a non null value, the calculator works as expected. I can't even test for Nulls b/c Python in the Field Calculator apparantly doesn't allow you to access the fields directly in the Codeblock like VBScript would.

Has anyone else ran into this? This seems like it would be a fairly common issue.
0 Kudos
4 Replies
JasonTipton
Occasional Contributor III
Ok... can't believe nobody else has ran into this, but....

I found not to use the FieldCalculator. Just create your own updateCursor. Make sure to include
from types import *


Then just test by
if type(c.field) is not NoneType:
Then, if you want to return a Null value:
c.field = None
All of this where c is your updateCursor and field is the field you are testing and setting.
0 Kudos
DavidStrip
New Contributor II
I just ran into this now. While I think I understand your solution, this seems like an odd behavior for the design of the Field Calculator. I would consider it a bug. Perhaps someone at ESRI can explain the reasoning for this behavior?
0 Kudos
ZdeněkJankovský
Esri Contributor
Isn't it this behavior?

NIM059424 - Null values in the feature class do not get replaced when using Python in the Field Calculator.

Seems to be fixed in 10.1.
0 Kudos
JasonTipton
Occasional Contributor III
Yes, I'm on 10 SP3 and it seems to be fixed

Isn't it this behavior?

NIM059424 - Null values in the feature class do not get replaced when using Python in the Field Calculator.

Seems to be fixed in 10.1.


I don't really use python with the field Calculator anymore, though. I had to switch to using cursors to get around the issue and haven't looked back. If you're going to go through all the trouble to create some code, might as well do it up right.
0 Kudos