jackc/pgx · error

cannot scan NULL into %#v

Error message

cannot scan NULL into %#v

What it means

Returned by ptrStructWrapper.ScanNull when a NULL composite is scanned into a pointer-to-struct target. The wrapper deliberately refuses to NULL out the caller's struct pointer, since silently replacing it could hide the NULL from the program.

Solutions

  1. Scan into a **struct (pointer to pointer) so NULL can be represented
  2. Use COALESCE or filter out NULL rows in the query
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at pgtype/builtin_wrappers.go:736 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of jackc/pgx@ec1a0befd2 (2026-08-04). Data as JSON: /api/errors/9d06cb245602a4de. Report an issue: GitHub.

Appendix: source

Thrown at pgtype/builtin_wrappers.go:736

	return w.s == nil
}

func (w structWrapper) Index(i int) any {
	if i >= len(w.exportedFields) {
		return fmt.Errorf("%#v only has %d public fields - %d is out of bounds", w.s, len(w.exportedFields), i)
	}

	return w.exportedFields[i].Interface()
}

// ptrStructWrapper implements CompositeIndexScanner for a pointer to a struct.
type ptrStructWrapper struct {
	s              any
	exportedFields []reflect.Value
}

func (w *ptrStructWrapper) ScanNull() error {
	return fmt.Errorf("cannot scan NULL into %#v", w.s)
}

func (w *ptrStructWrapper) ScanIndex(i int) any {
	if i >= len(w.exportedFields) {
		return fmt.Errorf("%#v only has %d public fields - %d is out of bounds", w.s, len(w.exportedFields), i)
	}

	return w.exportedFields[i].Addr().Interface()
}

type anySliceArrayReflect struct {
	slice reflect.Value
}

func (a anySliceArrayReflect) Dimensions() []ArrayDimension {
	if a.slice.IsNil() {
		return nil
	}

View on GitHub (pinned to ec1a0befd2)