0

我有一个项目使用 JDBC RestfulPreparedStatement 来访问 TDengine 数据库。如果我不传递参数,函数会以 NullPointerException 退出。

代码在这里:

public RestfulPreparedStatement(RestfulConnection conn, String database, String sql) {
super(conn, database);
this.rawSql = sql;

if (sql.contains("?")) {
    int parameterCnt = 0;
    for (int i = 0; i < sql.length(); i++) {
        if ('?' == sql.charAt(i)) {
            parameterCnt++;
        }
    }
    parameters = new Object[parameterCnt]; // not initial
    this.isPrepared = true;
}

引发 NullPointerException。错误信息如下:

[com.alibaba.druid.util.JdbcUtils.close(JdbcUtils.java:109)]
java.lang.NullPointerException: null
at com.taosdata.jdbc.rs.RestfulPreparedStatement.clearParameters(RestfulPreparedStatement.java:213)
at com.alibaba.druid.pool.DruidPooledConnection.closePoolableStatement(DruidPooledConnection.java:158)
at com.alibaba.druid.pool.DruidPooledPreparedStatement.close(DruidPooledPreparedStatement.java:201)
at com.alibaba.druid.util.JdbcUtils.close(JdbcUtils.java:98)
at com.alibaba.druid.pool.DruidConnectionHolder.reset(DruidConnectionHolder.java:323)
at com.alibaba.druid.pool.DruidDataSource.recycle(DruidDataSource.java:1917)
at com.alibaba.druid.pool.DruidPooledConnection.recycle(DruidPooledConnection.java:325)
at com.alibaba.druid.pool.DruidPooledConnection.close(DruidPooledConnection.java:270)
at org.springframework.jdbc.datasource.DataSourceUtils.doCloseConnection(DataSourceUtils.java:404)
at org.springframework.jdbc.datasource.DataSourceUtils.doReleaseConnection(DataSourceUtils.java:391)
at org.springframework.jdbc.datasource.DataSourceUtils.releaseConnection(DataSourceUtils.java:358)
at org.mybatis.spring.transaction.SpringManagedTransaction.close(SpringManagedTransaction.java:115)

我很困惑我应该做什么。请帮我。

4

0 回答 0