You answered your own question - if you have no products, then there will be no member function available. I think the bigger question would be why you’re hitting that line of code if you have no products available.
Surely that would be if ($_product == null) check? I know OP has solved the issue, but surely using a single equal-sign in this context would be incorrect?