Session.getAgentContext() returns null or crashes if the agent runs on "All Docs in Database" when the database is empty. You can work around this problem by opening and saving the appropriate agent context document in the AGENTRUNNER.NSF database.