{"record":{"id":"4d24a8b8800acce8","repo":"tursodatabase/turso","slug":"failed-to-convert-sql-into-bytes","errorCode":null,"errorMessage":"Failed to convert {sql} into bytes","messagePattern":"Failed to convert (.+?) into bytes","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"error","filePath":"bindings/java/src/main/java/tech/turso/core/TursoConnection.java","lineNumber":135,"sourceCode":"  /**\n   * Compiles an SQL statement with optional transaction start check.\n   *\n   * @param sql An SQL statement.\n   * @param checkTransaction Whether to check and start transaction if needed.\n   * @return Pointer to statement.\n   * @throws SQLException if a database access error occurs.\n   */\n  private TursoStatement prepare(String sql, boolean checkTransaction) throws SQLException {\n    logger.trace(\"DriverManager [{}] [SQLite EXEC] {}\", Thread.currentThread().getName(), sql);\n\n    // Ensure transaction is started if needed (lazy transaction start)\n    if (checkTransaction) {\n      ensureTransactionStarted(sql);\n    }\n\n    byte[] sqlBytes = stringToUtf8ByteArray(sql);\n    if (sqlBytes == null) {\n      throw new SQLException(\"Failed to convert \" + sql + \" into bytes\");\n    }\n    return new TursoStatement(sql, prepareUtf8(connectionPtr, sqlBytes));\n  }\n\n  private native long prepareUtf8(long connectionPtr, byte[] sqlUtf8) throws SQLException;\n\n  // TODO: check whether this is still valid for turso\n  /**\n   * Checks whether the type, concurrency, and holdability settings for a {@link ResultSet} are\n   * supported by the SQLite interface. Supported settings are:\n   *\n   * <ul>\n   *   <li>type: {@link ResultSet#TYPE_FORWARD_ONLY}\n   *   <li>concurrency: {@link ResultSet#CONCUR_READ_ONLY})\n   *   <li>holdability: {@link ResultSet#CLOSE_CURSORS_AT_COMMIT}\n   * </ul>\n   *\n   * @param resultSetType the type setting.","sourceCodeStart":117,"sourceCodeEnd":153,"githubUrl":"https://github.com/tursodatabase/turso/blob/244cde92a7df7f9b8b8b7a4075c35a12977e303e/bindings/java/src/main/java/tech/turso/core/TursoConnection.java#L117-L153","documentation":"prepare() converts the SQL string to UTF-8 bytes via stringToUtf8ByteArray, which returns null only when the input string is null (UTF-8 encoding of a Java String cannot fail). So this message, despite its generic wording, effectively means prepare(null) — a null SQL string reached statement compilation.","triggerScenarios":"Connection.prepareStatement(null) or any internal path (createStatement().execute(null), executeInternal with null) that passes a null SQL string; SQL built from a null-valued variable or a map lookup that returned null.","commonSituations":"SQL read from a config/annotation/map where the key was missing; refactoring left a null default; conditional SQL builder returned null instead of an empty string.","solutions":["Null-check SQL strings before prepare/execute and fail with a meaningful application error","Trace where the null came from — the exception message conveniently embeds the (null) SQL","Default builders to empty or throw early during construction, not at JDBC boundary"],"exampleFix":"// before\nString sql = queries.get(queryName); // null when key missing\nPreparedStatement ps = conn.prepareStatement(sql);\n\n// after\nString sql = Objects.requireNonNull(queries.get(queryName), \"missing query: \" + queryName);\nPreparedStatement ps = conn.prepareStatement(sql);","handlingStrategy":"validation","validationCode":"// validate before hitting the JDBC boundary\nString sql = queries.get(queryName);\nObjects.requireNonNull(sql, () -> \"SQL for '\" + queryName + \"' is missing/null\");\ntry (PreparedStatement ps = conn.prepareStatement(sql)) { /* ... */ }","typeGuard":null,"tryCatchPattern":"try {\n  return conn.prepareStatement(sql);\n} catch (SQLException e) {\n  if (e.getMessage().startsWith(\"Failed to convert \")) throw new IllegalArgumentException(\"null/invalid SQL passed to prepare\", e);\n  throw e;\n}","preventionTips":["Null-check SQL built from maps/config/annotations at construction time with a descriptive message","Prefer empty-string-free builders that always produce a real statement or throw early","Note this error specifically indicates a null SQL string — UTF-8 conversion of a real String cannot fail"],"tags":["null-safety","sql","java","jdbc"],"backgroundTag":"null-argument","analyzedSha":"244cde92a7df7f9b8b8b7a4075c35a12977e303e","analyzedAt":"2026-08-20T07:02:18.389Z","contentChangedAt":"2026-08-20T07:02:18.389Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}