TheAlgorithms/Java · error · IllegalArgumentException

Segment list must not be null

Error message

Segment list must not be null

What it means

Thrown by BentleyOttmann.findIntersections(List<Segment> segments) when the segments list itself is null. The method iterates the list to seed its event queue; a null list NPEs immediately, so it is rejected up front. Message: 'Segment list must not be null'.

Source

Thrown at src/main/java/com/thealgorithms/geometry/BentleyOttmann.java:164

            }
            return cmp;
        }
    }

    /**
     * Finds all intersection points among a set of line segments.
     *
     * <p>An intersection point is reported when two or more segments cross or touch.
     * For overlapping segments, only actual crossing/touching points are reported,
     * not all points along the overlap.</p>
     *
     * @param segments list of line segments represented as pairs of points
     * @return a set of intersection points where segments meet or cross
     * @throws IllegalArgumentException if the list is null or contains null points
     */
    public static Set<Point2D.Double> findIntersections(List<Segment> segments) {
        if (segments == null) {
            throw new IllegalArgumentException("Segment list must not be null");
        }

        Segment.segmentCounter = 0; // Reset counter
        Set<Point2D.Double> intersections = new HashSet<>();
        PriorityQueue<Event> eventQueue = new PriorityQueue<>();
        TreeSet<Segment> status = new TreeSet<>(new StatusComparator());
        Map<Point2D.Double, Event> eventMap = new HashMap<>();

        // Initialize event queue with segment start and end points
        for (Segment s : segments) {
            Point2D.Double left = s.leftPoint();
            Point2D.Double right = s.rightPoint();

            Event startEvent = getOrCreateEvent(eventMap, left, EventType.START);
            startEvent.addSegment(s);

            Event endEvent = getOrCreateEvent(eventMap, right, EventType.END);
            endEvent.addSegment(s);

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pass Collections.emptyList() instead of null when there are no segments.
  2. Ensure the segment-source method never returns null (return empty list).
  3. Guard with Objects.requireNonNull at the boundary.

Example fix

// before
Set<Point2D.Double> pts = BentleyOttmann.findIntersections(maybeNullList);

// after
List<Segment> segs = maybeNullList != null ? maybeNullList : Collections.emptyList();
Set<Point2D.Double> pts = BentleyOttmann.findIntersections(segs);
Defensive patterns

Strategy: validation

Validate before calling

List<Segment> segs = segments != null ? segments : Collections.emptyList();
BentleyOttmann.findIntersections(segs);

Type guard

segments != null

Prevention

When it happens

Trigger: Passing null for the segments list; a list field left null because no segments were constructed; chaining from a method that returns null on no input.

Common situations: Geometry pipelines where the segment-extraction step produced nothing and returned null instead of an empty list; tests that forgot to build the list.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/31d91831c3e310ea. Report an issue: GitHub.