我在方法中收到NullPointerException。我唯一可疑的部分是我可能没有在准备好的语句中添加任何参数。代码如下

try {
  String sql = "update myTable set Name = 'valid' where startDate = ?";
  myPrepStatement = conn.prepareStatement(sql);
  for(Date date : dateList) {
    myPrepStatement.setDate(1,date);
    myPrepStatement.addBatch();
  }
  myPrepStatement.executeBatch();
}
catch (Exception e){

}


假设conn设置正确,并且所有其他变量都已正确初始化。如果dateList为空,我是否有可能收到NullPointerException?
谢谢

最佳答案

documentation

public interface PreparedStatement extends Statement



  如果dateList为空,我是否可以收到
  空指针异常?


因此,如果您想100%确定,则应该深入研究JDBC驱动程序实现代码。

例如,MariaDB JDBC驱动程序实现:

public void addBatch() throws SQLException {
    if (this.batchPreparedStatements == null) {
        this.batchPreparedStatements = new ArrayList();
    }
    this.batchPreparedStatements.add(new MySQLPreparedStatement(
            this.connection, this.sql, this.dQuery,
            this.useFractionalSeconds));
}


MySQL JDBC驱动程序实现:

public synchronized void addBatch()
    throws SQLException
  {
    if (this.batchedArgs == null) {
      this.batchedArgs = new ArrayList();
    }

    for (int i = 0; i < this.parameterValues.length; ++i) {
      checkAllParametersSet(this.parameterValues[i], this.parameterStreams[i], i);
    }

    this.batchedArgs.add(new BatchParams(this.parameterValues, this.parameterStreams, this.isStream, this.streamLengths, this.isNull));
  }

09-05 20:54