redis/jedis · error · InstantiationError

Must not instantiate this class

Error message

Must not instantiate this class

What it means

QueryBuilders is a static utility class whose only constructor is private and deliberately throws java.lang.InstantiationError. It exists purely as a namespace for static query-node factory methods (interse ction, union, disjunction, tag, etc.), so instantiation is forbidden by design. Reaching this error means the class itself was constructed reflectively or via an unsupported path, since the compiler normally prevents it.

Solutions

  1. Use the static factory methods directly, e.g. QueryBuilders.interse ction(...), instead of instantiating the class.
  2. If reflective instantiation is intentional, exclude utility classes like QueryBuilders from the reflection-based tooling/instantiation list.
  3. If a wrapper is needed, write your own delegating class rather than subclassing or constructing QueryBuilders.

Example fix

// before
QueryBuilders qb = new QueryBuilders(); // InstantiationError (e.g. via reflection)
// after
Query.Node node = QueryBuilders.tag("foo", "bar"); // use static API
Defensive patterns

Strategy: try-catch

Validate before calling

if (clazz == QueryBuilders.class) {
  throw new IllegalStateException("QueryBuilders is a utility class; use its static methods");
}

Type guard

boolean isUtilityClass(Class<?> c) {
  return c == QueryBuilders.class || c.getDeclaredConstructors().length == 1 && isPrivate(c.getDeclaredConstructors()[0]);
}

Try / catch

try {
  ctor.setAccessible(true);
  ctor.newInstance();
} catch (java.lang.InstantiationError e) {
  log.warn("{} is a static utility class; use static factory methods", e.getMessage());
}

Prevention

When it happens

Trigger: Calling new QueryBuilders() will not compile; this error is thrown only when the private constructor is invoked via reflection (Class.newInstance / Constructor.newInstance), serialization frameworks, bytecode tools, or wrappers that bypass access checks.

Common situations: Reflection-based test utilities or coverage/instrumentation tooling (e.g. JaCoCo, Mockito inline mocking) instantiating all classes in a package; generic factory/IOC code that reflects over constructors of utility classes; code generators producing `new QueryBuilders()`.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of redis/jedis@6dac31d4c2 (2026-09-08). Data as JSON: /api/errors/c99194409a2273de. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/redis/clients/jedis/search/querybuilder/QueryBuilders.java:15

package redis.clients.jedis.search.querybuilder;

import java.util.Arrays;

import static redis.clients.jedis.search.querybuilder.Values.value;

/**
 * Created by mnunberg on 2/23/18.
 *
 * This class contains methods to construct query nodes. These query nodes can be added to parent
 * query nodes (building a chain) or used as the root query node.
 */
public class QueryBuilders {
  private QueryBuilders() {
    throw new InstantiationError("Must not instantiate this class");
  }

  /**
   * Create a new intersection node with child nodes. An intersection node is true if all its
   * children are also true
   *
   * @param n sub-condition to add
   * @return The node
   */
  public static QueryNode intersect(Node... n) {
    return new IntersectNode().add(n);
  }

  /**
   * Create a new intersection node with a field-value pair.
   *
   * @param field The field that should contain this value. If this value is empty, then any field
   * will be checked.

View on GitHub (pinned to 6dac31d4c2)