{"record":{"id":"d828629f52201d3d","repo":"apache/beam","slug":"numbuckets-should-be-greater-than-zero-d","errorCode":null,"errorMessage":"numBuckets should be greater than zero: %d","messagePattern":"numBuckets should be greater than zero: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/util/HistogramData.java","lineNumber":658,"sourceCode":"    public abstract int hashCode();\n  }\n\n  @AutoValue\n  public abstract static class LinearBuckets implements BucketType {\n    public abstract double getStart();\n\n    public abstract double getWidth();\n\n    @Override\n    public abstract int getNumBuckets();\n\n    public static LinearBuckets of(double start, double width, int numBuckets) {\n      if (width <= 0) {\n        throw new IllegalArgumentException(\n            String.format(\"width should be greater than zero: %f\", width));\n      }\n      if (numBuckets <= 0) {\n        throw new IllegalArgumentException(\n            String.format(\"numBuckets should be greater than zero: %d\", numBuckets));\n      }\n      return new AutoValue_HistogramData_LinearBuckets(start, width, numBuckets);\n    }\n\n    @Override\n    public int getBucketIndex(double value) {\n      return DoubleMath.roundToInt((value - getStart()) / getWidth(), RoundingMode.FLOOR);\n    }\n\n    @Override\n    public double getBucketSize(int index) {\n      return getWidth();\n    }\n\n    @Override\n    public double getAccumulatedBucketSize(int endIndex) {\n      return getWidth() * endIndex;","sourceCodeStart":640,"sourceCodeEnd":676,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/util/HistogramData.java#L640-L676","documentation":"LinearBuckets.of(start, width, numBuckets) requires at least one bucket. Passing numBuckets <= 0 yields a histogram with no buckets, which cannot be encoded or populated, so this IllegalArgumentException is thrown.","triggerScenarios":"Calling LinearBuckets.of(start, width, numBuckets) with numBuckets zero or negative — typically from an uninitialized/zero-default config value or a division-derived bucket count that rounds to 0.","commonSituations":"Config where numBuckets defaults to 0 meaning 'not set'; integer division truncation producing 0; user input not validated before building bucket definitions.","solutions":["Ensure numBuckets >= 1 before calling of()","Treat 0/negative config values as unset and apply a default bucket count","Validate at configuration load time rather than at histogram construction"],"exampleFix":"// before\nint numBuckets = config.get(\"buckets\", 0);\nLinearBuckets.of(start, width, numBuckets);\n// after\nint numBuckets = Math.max(1, config.get(\"buckets\", DEFAULT_NUM_BUCKETS));\nLinearBuckets.of(start, width, numBuckets);","handlingStrategy":"validation","validationCode":"if (numBuckets <= 0) {\n  throw new IllegalArgumentException(\"numBuckets must be positive, got: \" + numBuckets);\n}","typeGuard":null,"tryCatchPattern":"try {\n  buckets = LinearBuckets.of(start, width, numBuckets);\n} catch (IllegalArgumentException e) {\n  throw new IllegalArgumentException(\"Invalid bucket count: \" + numBuckets, e);\n}","preventionTips":["Default unset bucket counts to a positive constant","Beware integer division truncating bucket counts to 0"],"tags":["java","histogram","argument-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}