{"record":{"id":"aeb9c43707614c99","repo":"apache/flink","slug":"cannot-retrieve-right-value-on-a-left","errorCode":null,"errorMessage":"Cannot retrieve Right value on a Left","messagePattern":"Cannot retrieve Right value on a Left","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/types/Either.java","lineNumber":101,"sourceCode":"     * @param <R> the type of Right\n     */\n    public static class Left<L, R> extends Either<L, R> {\n        private L value;\n\n        private Right<L, R> right;\n\n        public Left(L value) {\n            this.value = java.util.Objects.requireNonNull(value);\n        }\n\n        @Override\n        public L left() {\n            return value;\n        }\n\n        @Override\n        public R right() {\n            throw new IllegalStateException(\"Cannot retrieve Right value on a Left\");\n        }\n\n        /**\n         * Sets the encapsulated value to another value\n         *\n         * @param value the new value of the encapsulated value\n         */\n        public void setValue(L value) {\n            this.value = value;\n        }\n\n        @Override\n        public boolean equals(Object object) {\n            if (object instanceof Left<?, ?>) {\n                final Left<?, ?> other = (Left<?, ?>) object;\n                return value.equals(other.value);\n            }\n            return false;","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/types/Either.java#L83-L119","documentation":"Either is Flink's disjoint-union type with subclasses Left and Right. Left.right() always throws IllegalStateException('Cannot retrieve Right value on a Left') because a Left instance carries no right value; the value is a required non-null field set in the constructor.","triggerScenarios":"Calling either.right() on an instance that is a Left — i.e. consuming the value without first checking which side is present (either.isLeft() / either.isRight()).","commonSituations":"Iterating a heterogeneous result collection where some elements are Left (e.g. failures/misses) and unconditionally calling right(); code written against the happy path where all elements were Right; Scala/Java interop where pattern matching was skipped.","solutions":["Check the side before unwrapping: if (either.isRight()) { R v = either.right(); } else { L l = either.left(); }.","Handle both branches explicitly — Left very often models an error/miss case that must not be silently dropped.","Prefer Flink's ifLeft/ifRight lambdas (when available) or Java's Optional-returning helpers to force branch handling."],"exampleFix":"// before\nString v = either.right(); // IllegalStateException when element is a Left\n\n// after\nif (either.isRight()) {\n    String v = either.right();\n} else {\n    Throwable err = either.left();\n    // handle failure case\n}","handlingStrategy":"type-guard","validationCode":"if (either.isRight()) {\n    String v = either.right();\n} else {\n    Throwable err = either.left();\n}","typeGuard":"static <L, R> boolean hasRight(Either<L, R> e) {\n    return e.isRight(); // true only when safe to call e.right()\n}","tryCatchPattern":null,"preventionTips":["Never unwrap an Either without first testing isLeft()/isRight().","Handle the Left branch explicitly — it usually models the failure/miss case.","Prefer exhaustive pattern matching (Scala match, Java 17 pattern matching for switch) so the compiler enforces both branches."],"tags":["types","either","flink-core","api-misuse"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}