apache/beam · error

coder must not be nil

Error message

coder must not be nil

What it means

NewW in sdks/go/pkg/beam/core/graph/coder/coder.go panics with "coder must not be nil" when the element coder passed for a WindowedValue coder is nil. A WindowedValue coder wraps an element coder plus a window coder, so a nil element coder would produce a broken composite. The constructor treats nil as a caller programming bug and fails fast.

Source

Thrown at sdks/go/pkg/beam/core/graph/coder/coder.go:413

// NewIntervalWindowCoder returns a new IntervalWindow coder using the built-in scheme.
func NewIntervalWindowCoder() *Coder {
	return &Coder{Kind: IW, T: typex.New(reflect.TypeOf((*struct{ Start, End int64 })(nil)).Elem())}
}

// IsW returns true iff the coder is for a WindowedValue.
func IsW(c *Coder) bool {
	return c.Kind == WindowedValue
}

// NewPI returns a PaneInfo coder
func NewPI() *Coder {
	return &Coder{Kind: PaneInfo, T: typex.New(typex.PaneInfoType)}
}

// NewW returns a WindowedValue coder for the window of elements.
func NewW(c *Coder, w *WindowCoder) *Coder {
	if c == nil {
		panic("coder must not be nil")
	}
	if w == nil {
		panic("window must not be nil")
	}

	return &Coder{
		Kind:       WindowedValue,
		T:          typex.NewW(c.T),
		Window:     w,
		Components: []*Coder{c},
	}
}

// NewPW returns a ParamWindowedValue coder for the window of elements.
func NewPW(c *Coder, w *WindowCoder) *Coder {
	if c == nil {
		panic("coder must not be nil")
	}

View on GitHub (pinned to 12126d8942)

Solutions

  1. Build the element coder before NewW, e.g. c := coder.NewJ(T); wc := coder.NewW(c, coder.NewGlobalWindow()).
  2. Fix the producer of the nil coder: handle error/empty returns from any (coder, err) lookup.
  3. Add an explicit nil check at the call site to fail closer to the origin.

Example fix

// before
var c *coder.Coder
wc := coder.NewW(c, coder.NewGlobalWindow()) // panics
// after
c := coder.NewJ(reflect.TypeOf(""))
wc := coder.NewW(c, coder.NewGlobalWindow())
Defensive patterns

Strategy: validation

Validate before calling

if c == nil {
    return nil, errors.New("element coder must be initialized before NewW")
}
wc := coder.NewW(c, coder.NewGlobalWindow())

Type guard

func isCoderReady(c *coder.Coder) bool { return c != nil && c.T != nil }

Try / catch

func newWSafe(c *coder.Coder, w *coder.WindowCoder) (wc *coder.Coder, err error) {
    defer func() {
        if r := recover(); r != nil {
            err = fmt.Errorf("NewW failed: %v", r)
        }
    }()
    return coder.NewW(c, w), nil
}

Prevention

When it happens

Trigger: Calling coder.NewW(nil, w) directly, or passing a coder variable that was never initialized — e.g. a registry/map lookup that returned nil with its error ignored.

Common situations: Test helpers (TestCoder_String, TestCoders) and pipeline graph builders composing coders from maps where the element-coder key was missing; refactors changing initialization order.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/0ad6fcdf754fc935. Report an issue: GitHub.