Catch error - Cannot read property 'getChild' of null

256 views
Skip to first unread message

urwa shabir

unread,
Apr 6, 2020, 1:11:30 AM4/6/20
to Google Apps Script Community
I have this piece of function in apps script that runs on a time trigger every day.  This fetches data from a SOAP API.

function getDataItems(response, endpoint, responseName, responseChildName) {
 
  var document = XmlService.parse(response);
  var root = document.getRootElement();
  var soapenv = XmlService.getNamespace('http://schemas.xmlsoap.org/soap/envelope/');
  var ns1 = XmlService.getNamespace('https://api.trafficvance.com/?v3='+endpoint);
 
  var items = root.getChild('Body', soapenv)
                  .getChild(responseName, ns1)
                  .getChild('return')
                  .getChild(responseChildName)
                  .getChildren('item');
 
  return items;
}

Sometimes (randomly) it fails with the following error:
"TypeError: Cannot read property 'getChild' of null at getDataItems(Code:140:19)"


And then I manually run it for the date it failed and it runs successfully. What is the best way to catch this error and run the function again if it fails. So that the script can run independently without any human involvement.

I would really appreciate any help. Thanks

Alan Wells

unread,
Apr 6, 2020, 8:34:11 AM4/6/20
to Google Apps Script Community
The code has 4 getChild uses and we don't know what line 140 is.  And we don't know the value of "root"  If you had error handling that got the value of "root" when the error occurred, then you could look at it, and use it for debugging purposes.  Either "root" is null, or one of the first 3 getChild() methods is returning null instead of a value.  Which one of those "getChild" lines is line 140?

urwa shabir

unread,
Apr 6, 2020, 8:59:06 AM4/6/20
to Google Apps Script Community
I am sorry, I should have mentioned this. Line 140 is ".getChild('return')".
Basically what I want to know is what the standard to catch such errors. OR should I check value for every "getChild()".

Thanks for your response.

Alan Wells

unread,
Apr 6, 2020, 9:24:23 AM4/6/20
to Google Apps Script Community
You can use a try/catch to catch an error.  Or you could break up your chain of methods and test for null or a falsey value.  If you want your code to continue from a fatal error, then you'll need to catch the error.  try/catch can be put around the entire contents of a function, or just one line, and they can be nested.  It depends on what you want to do.  If there is some alternative code that you can run if

.getChild(responseName, ns1)

returns null to somehow continue the processing, then your catch block could do that.  But, if your "root" content should be fixed in order to get the correct end result, then you might want to end the code.

You could put:

  var items = root.getChild('Body', soapenv)
                  .getChild(responseName, ns1);

On it's own line, then check for "items" being null.

if (items === null) {
  //do something here before the code creates an error

urwa shabir

unread,
Apr 7, 2020, 2:10:15 AM4/7/20
to Google Apps Script Community
Thank you so much. This makes so much sense.

- U. Shabbir
Reply all
Reply to author
Forward
0 new messages