github/copilot-sdk · error · NullPointerException

policy must not be null

Error message

policy must not be null

What it means

setEventErrorPolicy stores the handler that decides how session event-processing errors are treated. A null policy would silently disable error handling, so the method explicitly throws NullPointerException with "policy must not be null".

Solutions

  1. Pass a non-null policy; to disable custom handling pass a benign default policy such as EventErrorPolicy.IGNORE (or library default)
  2. Keep a static NO_OP EventErrorPolicy constant for clearing
  3. Initialize policy fields with a default instead of null

Example fix

// before
session.setEventErrorPolicy(null);
// after
session.setEventErrorPolicy(EventErrorPolicy.IGNORE); // or your default policy
Defensive patterns

Strategy: validation

Validate before calling

if (policy == null) { policy = EventErrorPolicy.IGNORE; } // before calling

Try / catch

try {
    session.setEventErrorPolicy(policy);
} catch (NullPointerException e) {
    if ("policy must not be null".equals(e.getMessage())) {
        session.setEventErrorPolicy(EventErrorPolicy.IGNORE);
    }
}

Prevention

When it happens

Trigger: Calling session.setEventErrorPolicy(null), typically while attempting to "remove" the policy or passing an uninitialized field.

Common situations: Refactors that changed an EventErrorPolicy field default to null; clearing handlers by passing null instead of a no-op policy; configuration objects with missing policy values.

Related errors


AI-assisted analysis of github/copilot-sdk@cd8cf15dc3 (2026-09-09). Data as JSON: /api/errors/f603b6263b8ff6ed. Report an issue: GitHub.

Appendix: source

Thrown at java/sdk/src/main/java/com/github/copilot/CopilotSession.java:501

     * <pre>{@code
     * // Opt-in to suppress errors (continue dispatching despite errors)
     * session.setEventErrorPolicy(EventErrorPolicy.SUPPRESS_AND_LOG_ERRORS);
     * session.setEventErrorHandler((event, ex) -> logger.error("Handler failed, continuing: {}", ex.getMessage(), ex));
     * }</pre>
     *
     * @param policy
     *            the error policy (default is
     *            {@link EventErrorPolicy#PROPAGATE_AND_LOG_ERRORS})
     * @throws IllegalStateException
     *             if this session has been terminated
     * @see EventErrorPolicy
     * @see #setEventErrorHandler(EventErrorHandler)
     * @since 1.0.8
     */
    public void setEventErrorPolicy(EventErrorPolicy policy) {
        ensureNotTerminated();
        if (policy == null) {
            throw new NullPointerException("policy must not be null");
        }
        this.eventErrorPolicy = policy;
    }

    /**
     * Sends a simple text message to the Copilot session.
     * <p>
     * This is a convenience method equivalent to
     * {@code send(new MessageOptions().setPrompt(prompt))}.
     *
     * @param prompt
     *            the message text to send
     * @return a future that resolves with the message ID assigned by the server
     * @throws IllegalStateException
     *             if this session has been terminated
     * @see #send(MessageOptions)
     */
    public CompletableFuture<String> send(String prompt) {

View on GitHub (pinned to cd8cf15dc3)