You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Snowflake-JDBC执行查询触发NullPointerException问题求助

问题描述

尝试通过Snowflake JDBC执行简单查询以测试连通性,Maven依赖配置如下:

<dependencies>
    <!-- https://mvnrepository.com/artifact/net.snowflake/snowflake-jdbc -->
    <dependency>
        <groupId>net.snowflake</groupId>
        <artifactId>snowflake-jdbc</artifactId>
        <version>3.15.0</version>
    </dependency>
</dependencies>

已成功创建连接且凭证验证有效,但执行最简单的查询时触发net.snowflake.client包的NullPointerException。执行的Kotlin代码如下:

val connection = DriverManager.getConnection(dbUrl, connectionProperties)

connection.prepareStatement("select 1")
          .executeQuery()
          .getInt(1)

错误信息:

java.lang.NullPointerException: Cannot invoke "java.util.List.size()" because "this.currentConverters" is null
at net.snowflake.client.jdbc.ArrowResultChunk$ArrowChunkIterator.getCurrentConverter(ArrowResultChunk.java:462)
at net.snowflake.client.core.SFArrowResultSet.getInt(SFArrowResultSet.java:408)
at net.snowflake.client.jdbc.SnowflakeResultSetV1.getInt(SnowflakeResultSetV1.java:192)

疑惑:入门级程序触发库内部错误,如何解决?是否缺少其他依赖?

解决方法
  • 修正ResultSet使用逻辑:executeQuery()返回的ResultSet默认光标位于第一行数据之前,必须先调用next()方法移动到有效行,才能调用getInt()等取值方法。修正后的代码:
val connection = DriverManager.getConnection(dbUrl, connectionProperties)
val statement = connection.prepareStatement("select 1")
val resultSet = statement.executeQuery()
if (resultSet.next()) {
    val value = resultSet.getInt(1)
    // 处理查询结果
}
// 务必关闭资源(可使用try-with-resources自动管理)
resultSet.close()
statement.close()
connection.close()
  • 升级JDBC驱动版本:3.15.0是较旧的版本,存在已知的Arrow结果集相关bug。建议升级到最新稳定版,修改Maven依赖:
<dependency>
    <groupId>net.snowflake</groupId>
    <artifactId>snowflake-jdbc</artifactId>
    <version>3.20.0</version> <!-- 替换为最新稳定版本 -->
</dependency>
  • 补充Arrow相关依赖:部分版本的Snowflake JDBC驱动需要额外的Apache Arrow依赖支持,若升级后仍有问题,可添加以下兼容版本的依赖:
<dependency>
    <groupId>org.apache.arrow</groupId>
    <artifactId>arrow-vector</artifactId>
    <version>10.0.1</version>
</dependency>
<dependency>
    <groupId>org.apache.arrow</groupId>
    <artifactId>arrow-format</artifactId>
    <version>10.0.1</version>
</dependency>

内容的提问来源于stack exchange,提问作者Jake

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 08:47:05