prevent panic if you use API in a wrong way

Fixes #2538