Test for Null values Field Calculator

894
4
02-07-2013 12:29 PM
AdamCox1
Occasional Contributor II
Ok, I just finished reading a bunch of threads on this, but need to double check this issue.  I want to put something like this
def i(x):
  if x is None:
    return "Used to be Null"
  else:
    return x

into the code-block, but I've tried all sorts of variations that have been suggested, x == None, x == "", if not x, but nothing has produced any change in the values.

Am I missing something, or is this just not going to work and I'll have to use update cursors to perform operations like this?

I'm using 10.0 SP5.

Thank you!
Tags (2)
0 Kudos
4 Replies
MathewCoyle
Frequent Contributor
May be a bug, there were a lot of issues with null values in 10.0 I believe.

NIM079575 - Field Calculator does not recognize <Null>

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

NIM086737 - The Select by Attributes tool using "IS NULL" on a joined field does not return selections in ArcGIS 10 SP5.

NIM063379 - A Field Calculator error dialog opens in PythonWin for an assignment of a null value to a text field in a shapefile.


I believe this was the work around.

def i(x):
    var = x
    if var:
        return "Used to be Null"
    else:
        return var
0 Kudos
AdamCox1
Occasional Contributor II
Hello and thanks for the reply,

Unfortunately, that work around still produces nothing.  I happened to be working with a joined table, but after removing the join, still no luck.  It doesn't even work to flip it around and say
def i(x):
  var = x
  if var:
    return var
  else:
    return "not null anymore"
 
Because I'm starting with all Null values, I think my first step will just be to write "NULL" to the entire field, which works, and then query against the string "NULL".  Any other ideas would be welcome, too, of course.

Thanks!
0 Kudos
MathewCoyle
Frequent Contributor
That is odd, it works for me.

Edit: Sorry no, I was using a cursor.

rows = arcpy.UpdateCursor(layer)
for row in rows:
    var = row.AREAESTIMATED
    if var:
        row.TAG = 1
    else:
        row.TAG = 0
    rows.updateRow(row)
0 Kudos
forestknutsen1
MVP Regular Contributor
I also have had this problem, ultimately I gave up on the field calculator and made a stand alone python program to to the job.

import arcpy, os

arcpy.env.workspace = "Z:\myTest.gdb" #path to your geodatabase

rows = arcpy.UpdateCursor("test") #test is the name of the feature class you wish to work with 


for row in rows:
    if row.test1 == None: #test1 would be the name of the field in the feature calss 
        row.test1 = "Used to be Null"

    rows.updateRow(row)

del row
del rows
0 Kudos