title: Null pointer exception when inserting row into Postgres
body: <p>I am querying a table in Postgres to see if a row with primary key values match those in a given <code>Map</code> exists. If there are no results, I want to insert a new row. I can do this just fine when I go the standard JDBC route of creating a prepared statement and explicitly setting all the column values, but I want a more generic approach with the help of Groovy's <code>Sql</code> class.</p>
  
  <p>This is my insert statement (the primary key columns are the first three):</p>
  
  <pre><code>insert into MY_TABLE
    (contract_month, contract_year, publish_date, open, high, low)
  values
    (10, 2015, 2015-09-15 00:00:00.0, 46.65, 46.9, 45.98)
  </code></pre>
  
  <p>The issue is that I'm getting a <code>NullPointerException</code> from the Postgres driver as soon as I insert a row:</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
      at org.postgresql.core.v3.SimpleParameterList.setNull(SimpleParameterList.java:142)
      at org.postgresql.jdbc2.AbstractJdbc2Statement.setNull(AbstractJdbc2Statement.java:1259)
      at org.postgresql.jdbc3.AbstractJdbc3Statement.setNull(AbstractJdbc3Statement.java:1499)
      at org.postgresql.jdbc4.AbstractJdbc4Statement.setNull(AbstractJdbc4Statement.java:85)
      at org.postgresql.jdbc2.AbstractJdbc2Statement.setObject(AbstractJdbc2Statement.java:2092)
      at org.postgresql.jdbc3g.AbstractJdbc3gStatement.setObject(AbstractJdbc3gStatement.java:38)
      at org.postgresql.jdbc4.AbstractJdbc4Statement.setObject(AbstractJdbc4Statement.java:48)
  </code></pre>
  
  <p>Digging into it, I find that an object called <code>ProtoConnection</code> is null in the <code>org.postgresql.core.v3.SimpleParameterList</code> class. What am I doing wrong?</p>
  
  <p>This is the wrapping code which creates the <code>Sql</code> object:</p>
  
  <pre><code>  private Main() {    
      final def headers = new HashMap&lt;Integer, String&gt;()
      final def record = new HashMap&lt;String, Object&gt;()
  
      Sql.withInstance(/* my db */) { final Sql sql -&gt;
        sql.withBatch {
          new URL(/* my url */).withReader {
            it.readLines().each {
              if (headers.isEmpty()) {
                setHeaders it, headers
              }
              else {
                processRecord it, headers, record
                storeRecord sql, record
              }
            }
          }
        }
      }
    }
  </code></pre>
  
  <p>And this is the database logic. The error occurs at the very last line:</p>
  
  <pre><code>def storeRecord = { final Sql sql, final Map&lt;String, Object&gt; record -&gt;
      final def connection = sql.connection
      final def metadata = connection.metaData
  
      final def columns = metadata.getColumns(null, null, MY_TABLE, null).with {
        final def result = []
        while (it.next()) {
          result &lt;&lt; it.getString(4)
        }
        result
      }
  
      final def primaryKeys = metadata.getPrimaryKeys(null, null, MY_TABLE).with {
        final def result = []
        while (it.next()) {
          result &lt;&lt; it.getString(4)
        }
        result
      }
  
      final def whereClause = primaryKeys.collect { final String pk -&gt;
        "$pk = ?.$pk"
      }
  
      final def select = """
          select ${columns.join(', ')}
            from MY_TABLE
           where ${whereClause.join(' and ')}
          """
  
      final def row = sql.firstRow(select, record)
  
      if (row) {
        println "Updating"
        // Not implemented yet
      }
      else {
        println "Inserting record: $record"
        final def insert = """
                  insert into MY_TABLE
                    (${columns.findAll { null != record[it] }.join(", ")})
                   values
                    (${columns.findAll { null != record[it] }.collect { record[it] }.join(", ")})
                  """
        println insert
        sql.executeInsert(insert, record)
      }
  </code></pre>