{"record":{"id":"70bf6ba72c220949","repo":"grpc/grpc-java","slug":"no-value-present","errorCode":null,"errorMessage":"No value present.","messagePattern":"No value present\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"api/src/main/java/io/grpc/StatusOr.java","lineNumber":59,"sourceCode":"  /** Construct from a non-Ok status. */\n  public static <T> StatusOr<T> fromStatus(Status status) {\n    StatusOr<T> result = new StatusOr<T>(checkNotNull(status, \"status\"), null);\n    checkArgument(!status.isOk(), \"cannot use OK status: %s\", status);\n    return result;\n  }\n\n  /** Returns whether there is a value. */\n  public boolean hasValue() {\n    return status == null;\n  }\n\n  /**\n   * Returns the value if set or throws exception if there is no value set. This method is meant\n   * to be called after checking the return value of hasValue() first.\n   */\n  public T getValue() {\n    if (status != null) {\n      throw new IllegalStateException(\"No value present.\");\n    }\n    return value;\n  }\n\n  /** Returns the status. If there is a value (which can be null), returns OK. */\n  public Status getStatus() {\n    return status == null ? Status.OK : status;\n  }\n\n  /**\n   * Note that StatusOr containing statuses, the equality comparision is delegated to\n   * {@link Status#equals} which just does a reference equality check because equality on\n   * Statuses is not well defined.\n   * Instead, do comparison based on their Code with {@link Status#getCode}.  The description and\n   * cause of the Status are unlikely to be stable, and additional fields may be added to Status\n   * in the future.\n   */\n  @Override","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/grpc/grpc-java/blob/64daddc1f3d1975670f769f3e97bde8b2ba32d25/api/src/main/java/io/grpc/StatusOr.java#L41-L77","documentation":"StatusOr.getValue() returns the contained value only when no error Status is set. If the StatusOr holds a status instead of a value (hasValue() == false), calling getValue() throws this IllegalStateException. The javadoc explicitly requires checking hasValue() first.","triggerScenarios":"Calling getValue() on a StatusOr produced from a failed operation — e.g. a failed name resolution (onResult), a lookup that returned an error status — without first checking hasValue(). Callers listed include resolvedAddresses, servers, clusterConfig on error paths.","commonSituations":"NameResolver results that failed with UNAVAILABLE or similar; xDS cluster config lookup failures; treating a StatusOr as always-successful after refactoring code that previously used exceptions or nullable returns.","solutions":["Always check hasValue() before calling getValue()","Use getStatus() to inspect and log the error status when hasValue() is false","Propagate the status (e.g. to the stream/stream-trailers) instead of unwrapping","In tests, assert hasValue() before asserting on the value"],"exampleFix":"// before\nAddress addr = statusOr.getValue(); // throws when resolution failed\n// after\nif (statusOr.hasValue()) {\n  Address addr = statusOr.getValue();\n} else {\n  Status s = statusOr.getStatus(); // handle/log UNAVAILABLE etc.\n}","handlingStrategy":"type-guard","validationCode":"boolean usable = statusOr != null && statusOr.hasValue();","typeGuard":"static <T> T getOrThrow(StatusOr<T> or, Supplier<? extends RuntimeException> onAbsent) {\n  return or.hasValue() ? or.getValue() : onAbsent.get();\n}","tryCatchPattern":"try {\n  value = statusOr.getValue();\n} catch (IllegalStateException e) {\n  Status s = statusOr.getStatus(); // inspect/handle error status\n}","preventionTips":["Always gate getValue() behind hasValue()","On the false branch read getStatus() and propagate/log it","Prefer a helper like getOrDefault() to avoid raw unwrap at call sites"],"tags":["grpc","optional-value","misuse"],"backgroundTag":"invalid-state-transition","analyzedSha":"64daddc1f3d1975670f769f3e97bde8b2ba32d25","analyzedAt":"2026-09-08T06:14:57.704Z","contentChangedAt":"2026-09-08T06:14:57.704Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}