I can’t replicate the error, so I’m speculating. The $location portion gets the menu ID from where the theme is supposed to store it. If the theme does not store menu IDs in the way expected, the locations function will return an empty or malformed array, causing the rest of the chain of functions to fail.
As it happens, we do not need the menu ID to get the menu object, we can use the name or slug as well. Since we have the menu name, there is no need to get the ID from the location, we can get the object directly by name. The example is based on code where only the location was known, not the name or slug, thus in that case the location step was necessary.