Jump to content
GMS, SMS, and WMS User Forum
Sign in to follow this  
stoyan

Calling AssignHydroID_archydro from a Python script raises a NullReferenceException exception

Recommended Posts

Hi, could someone please help me with the following issue: every time I call AssignHydroID_archydro from a Python script I get the following exception:

Traceback (most recent call last):

File "<string>", line 244, in run_nodebug

File "C:\masterImport.py", line 63, in <module>

gp.AssignHydroID_archydro(fc)

arcgisscripting.ExecuteError: System.NullReferenceException: Object reference not set to an instance of an object.

at ESRI.APWR.ArcHydroGPTools.AHAssignHydroID.Execute(IArray paramvalues, ITrackCancel trackcancel, IGPEnvironmentManager envMgr, IGPMessages message)

Failed to execute (AssignHydroID).

Here is the relevant part of the Python script:

-----------------------------------------------------------------------------------------------------

import arcgisscripting

gp = arcgisscripting.create(9.3)

gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Arc Hydro Tools.tbx")

gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Arc Hydro Groundwater Tools.tbx")

...various variable definitions

if gp.exists(gwd):

gp.OpenGroundwaterConfig_Groundwater(gwd, gwd_project_name)

else:

gp.CreateGroundwaterConfig_Groundwater(rasters, gwoutput, gwd_project_name, gwd)

if gp.exists(ahd):

gp.OpenArcHydroConfig_archydro(ahd, ahd_project_name)

else:

gp.CreateArcHydroConfig_archydro("Groundwater", rasters, gdb, ahd_project_name, ahd)

fc = r"C:\MMM.gdb\Framework\Well" #the fc lives within the fgdb configured in the .ahd and .gwd files

gp.AssignHydroID_archydro(fc) #this line throws the exception

#gp.AssignHydroID_archydro(fc,"") #these 2 combinations fail too

#gp.AssignHydroID_archydro(fc,"OVERWRITE_YES")

-----------------------------------------------------------------------------------------------------

I am running AHGW 2.0 on ArcGIS 9.3.1 SP1 (ArcInfo license).

The AHGW hardware dongle is plugged in.

Some observations:

-Running the Assign Hydro ID tool from a model works fine

-Trying to call a model, containing the Assign Hydro ID call, from a Python script fails with the same exception as above

-Calling Assign Hydro ID from a Python script on a feature class which has already had all HydroIDs populated works fine

-In summary: AssignHydroID only fails when called from a Python script and it needs to insert new ids.

Could someone please tell me how to get around this problem? I would be very appreciative as calling AssignHydroID_archydro from a script would automate my import process very nicely.

Much obliged,

Stoyan.

Share this post


Link to post
Share on other sites

This function reproduces part of the AssignHydroID tool's functionality.

It does not take into account individual layers as listed in the LAYERKEYTABLE, as I don't have a need for this.


def assignHydroID(apuniqueid, fc, overwrite=False):
'''apuniqueid: the location of the apuniqueid table
fc: the feature class which needs the HYDROID populated
overwrite: True if the existing HydroIDs in the fc should be overwritten

Populates the HYDROID in the given fc based on the lastest hydroidid from the apuniqueid table

Returns: True if successful
False if the table/fc does not exist

Throws: exception if the table/fc can not be open for editing/inserting
'''

import arcgisscripting
gp = arcgisscripting.create(9.3)

if not gp.exists(apuniqueid):
gp.adderror("APUNIQUEID: %s table does not exist"%apuniqueid)
return False
if not gp.exists(fc):
gp.adderror("%s does not exist"%fc)
return False

lastid = 0
exists = False #True if a HYDROID entry already exists in the apuniqueid table

rows = gp.searchcursor(apuniqueid,"IDNAME = 'HYDROID'")
row = rows.next()
if row:
lastid = row.LASTID
exists = True
del row, rows

rows = gp.updatecursor(fc,"","","HYDROID")
row = rows.next()
while row:
if row.isnull("HYDROID") or overwrite:
lastid+=1
row.HYDROID = lastid
rows.updaterow(row)
row = rows.next()
del row, rows

if exists: #a HYDROID entry already exists, so simply update the field
rows = gp.updatecursor(apuniqueid,"IDNAME = 'HYDROID'")
row = rows.next()
row.LASTID = lastid
rows.updaterow(row)
del row, rows
else: #need to make a new entry into the table for HYDROID
rows = gp.insertcursor(apuniqueid)
row = rows.newrow()
row.IDNAME = "HYDROID"
row.LASTID = str(lastid)
rows.insertrow(row)
del row, rows

gp.addmessage("Populated the HydroID field in %s."%fc)
gp.addmessage("The next hydroid would be %s"%lastid)
return True

Share this post


Link to post
Share on other sites

Hi,

Please notice that in version 3.0 of the Arc Hydro Groundwater tools there is a new Assign HydroID GW Tool:

http://www.archydrogw.com/ahgw/What%27s_New_in_Arc_Hydro_Groundwater_3.0.0

You can also call this tool tool from a python script.

With the new tool you don't need the Arc Hydro (surface water) tools or the ApFramework installed as you would in previous versions.

Gil

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...