elastic/elasticsearch · error · ParseException
expected , or ) but found: {}
Error message
expected , or ) but found: {} What it means
Thrown by WellKnownText.nextCloserOrComma when the token is neither ',' nor ')'. This helper is used to iterate over a list of sub-elements (vertices, rings, or collection members): it accepts a comma to continue the list or ')' to terminate it.
Source
Thrown at libs/geo/src/main/java/org/elasticsearch/geometry/utils/WellKnownText.java:780
if (nextWord(stream).equals(COMMA)) {
return COMMA;
}
throw new ParseException("expected " + COMMA + " but found: " + tokenString(stream), stream.lineno());
}
static String nextOpener(StreamTokenizer stream) throws IOException, ParseException {
if (nextWord(stream).equals(LPAREN)) {
return LPAREN;
}
throw new ParseException("expected " + LPAREN + " but found: " + tokenString(stream), stream.lineno());
}
static String nextCloserOrComma(StreamTokenizer stream) throws IOException, ParseException {
String token = nextWord(stream);
if (token.equals(COMMA) || token.equals(RPAREN)) {
return token;
}
throw new ParseException("expected " + COMMA + " or " + RPAREN + " but found: " + tokenString(stream), stream.lineno());
}
private static String getWKTName(Geometry geometry) {
return geometry.visit(new GeometryVisitor<String, RuntimeException>() {
@Override
public String visit(Circle circle) {
return "CIRCLE";
}
@Override
public String visit(GeometryCollection<?> collection) {
return "GEOMETRYCOLLECTION";
}
@Override
public String visit(Line line) {
return "LINESTRING";
}View on GitHub (pinned to db6a809a66)
Solutions
- Inspect the token at stream.lineno() and ensure the position holds either ',' (continue list) or ')' (end list).
- Remove stray characters and confirm the list grammar is well-formed.
- If iterating a collection, ensure each member is a complete geometry followed by , or ).
Example fix
// before: stray 'x' after a vertex where , or ) expected
Geometry g = WellKnownText.fromWKT("LINESTRING (0 0, 1 1 x)");
// after: proper separator or closer
Geometry g = WellKnownText.fromWKT("LINESTRING (0 0, 1 1)");
// or continue the list
Geometry g = WellKnownText.fromWKT("LINESTRING (0 0, 1 1, 2 2)"); Defensive patterns
Strategy: try-catch
Try / catch
try {
Geometry g = WellKnownText.fromWKT(wkt);
} catch (java.text.ParseException e) {
if (e.getMessage().contains("expected , or )")) {
// stray token in a coordinate/element list
}
throw e;
} Prevention
- Ensure each list element is followed by , or ).
- Remove stray characters and non-WKT content from coordinate lists.
When it happens
Trigger: After reading an element in a list, the parser expects either a comma (more elements) or a closing paren (end of list). If it finds anything else (a keyword, EOF, a number, a stray symbol), it throws. Example: "LINESTRING (0 0, 1 1 x)" where 'x' appears where , or ) is expected.
Common situations: Stray characters in coordinate lists; truncated WKT mid-list; a tokenizer desynchronized by a prior malformed token; non-WKT content embedded in the string.
Related errors
- When specifying 'Z' or 'M', coordinates must include three v
- maximum nested depth of 1000 exceeded
- coordinate dimensions do not match: {}
- expected word but found: {}
- invalid number found: {}
AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12).
Data as JSON: /api/errors/b240439f46c58ac9.
Report an issue: GitHub.