An additional reason you might see this error is if you're testing in the developer console. Instead, you need to run the code for when a button is clicked, for example.