Patch for 'updateNull' method null pointer exception

From: "Sean Gates" <sean(dot)gates(at)signiant(dot)com>
To: <pgsql-jdbc(at)postgresql(dot)org>
Subject: Patch for 'updateNull' method null pointer exception
Date: 2002-09-19 18:56:05
Message-ID: E6F241DAF915E24F9E34757342EE3E2202007F@ottas09a.ott.signiant.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Greetings,

A few weeks back I posted a question asking about an appropriate
implementation to fix the java.lang.NullPointerException being thrown by
the JDBC updateNull method, but received no response which I assumed
meant "no comment -- decide for yourself". The following patch uses a
somewhat brute force approach to solve the problem by using a "wrapper"
class to enclose values assigned to the updateable resultset value
hashtable so that the assignment of a null pointer value doesn't
generate an exception.

Please apply it to the beta 7.3 stream at your earliest convenience.

Thanks,

Sean Gates
Signiant Corporation
sgates(at)signiant(dot)com

P.S. Testing has been against PostgreSQL 7.2.1 database only.

------------------------------------------------------------------------
-----------------------

*** AbstractJdbc2ResultSet.java.orig Thu Sep 19 11:00:52 2002
--- AbstractJdbc2ResultSet.java Thu Sep 19 14:47:48 2002
***************
*** 623,629 ****
for ( int i = 1; keys.hasMoreElements(); i++)
{
String key = (String)
keys.nextElement();
! insertStatement.setObject(i,
updateValues.get( key ) );
}

insertStatement.executeUpdate();
--- 623,629 ----
for ( int i = 1; keys.hasMoreElements(); i++)
{
String key = (String)
keys.nextElement();
! insertStatement.setObject(i,
((ValueWrapper) updateValues.get(key)).value());
}

insertStatement.executeUpdate();
***************
*** 634,641 ****

long insertedOID =
((AbstractJdbc2Statement) insertStatement).getLastOID();

! updateValues.put("oid", new
Long(insertedOID) );
!
}

// update the underlying row to the new inserted
data
--- 634,640 ----

long insertedOID =
((AbstractJdbc2Statement) insertStatement).getLastOID();

! updateValues.put( "oid", new
ValueWrapper(new Long(insertedOID)) );
}

// update the underlying row to the new inserted
data
***************
*** 758,765 ****

doingUpdates = !onInsertRow;

! updateValues.put( fields[columnIndex - 1].getName(),
theData );
!
}


--- 757,763 ----

doingUpdates = !onInsertRow;

! updateValues.put( fields[columnIndex - 1].getName(), new
ValueWrapper(theData) );
}


***************
*** 774,781 ****
}

doingUpdates = !onInsertRow;
! updateValues.put( fields[columnIndex - 1].getName(), x
);
!
}


--- 772,778 ----
}

doingUpdates = !onInsertRow;
! updateValues.put( fields[columnIndex - 1].getName(), new
ValueWrapper(x) );
}


***************
*** 809,816 ****

doingUpdates = !onInsertRow;

! updateValues.put( fields[columnIndex - 1].getName(),
theData );
!
}


--- 806,812 ----

doingUpdates = !onInsertRow;

! updateValues.put( fields[columnIndex - 1].getName(), new
ValueWrapper(theData) );
}


***************
*** 827,834 ****
Driver.debug("updating boolean " +
fields[columnIndex - 1].getName() + "=" + x);

doingUpdates = !onInsertRow;
- updateValues.put( fields[columnIndex - 1].getName(), new
Boolean(x) );

}


--- 823,830 ----
Driver.debug("updating boolean " +
fields[columnIndex - 1].getName() + "=" + x);

doingUpdates = !onInsertRow;

+ updateValues.put( fields[columnIndex - 1].getName(), new
ValueWrapper(new Boolean(x)) );
}


***************
*** 841,847 ****
}

doingUpdates = true;
! updateValues.put( fields[columnIndex - 1].getName(),
String.valueOf(x) );
}


--- 837,844 ----
}

doingUpdates = true;
!
! updateValues.put( fields[columnIndex - 1].getName(), new
ValueWrapper(String.valueOf(x)) );
}


***************
*** 855,862 ****
}

doingUpdates = !onInsertRow;
- updateValues.put( fields[columnIndex - 1].getName(), x
);

}


--- 852,859 ----
}

doingUpdates = !onInsertRow;

+ updateValues.put( fields[columnIndex - 1].getName(), new
ValueWrapper(x) );
}


***************
*** 889,896 ****
}

doingUpdates = !onInsertRow;
- updateValues.put( fields[columnIndex - 1].getName(),
theData);

}


--- 886,893 ----
}

doingUpdates = !onInsertRow;

+ updateValues.put( fields[columnIndex - 1].getName(), new
ValueWrapper(theData) );
}


***************
*** 904,910 ****
}

doingUpdates = !onInsertRow;
! updateValues.put( fields[columnIndex - 1].getName(), x
);
}


--- 901,908 ----
}

doingUpdates = !onInsertRow;
!
! updateValues.put( fields[columnIndex - 1].getName(), new
ValueWrapper(x) );
}


***************
*** 920,927 ****
Driver.debug("updating double " +
fields[columnIndex - 1].getName() + "=" + x);

doingUpdates = !onInsertRow;
- updateValues.put( fields[columnIndex - 1].getName(), new
Double(x) );

}


--- 918,925 ----
Driver.debug("updating double " +
fields[columnIndex - 1].getName() + "=" + x);

doingUpdates = !onInsertRow;

+ updateValues.put( fields[columnIndex - 1].getName(), new
ValueWrapper(new Double(x)) );
}


***************
*** 938,945 ****

doingUpdates = !onInsertRow;

! updateValues.put( fields[columnIndex - 1].getName(), new
Float(x) );
!
}


--- 936,942 ----

doingUpdates = !onInsertRow;

! updateValues.put( fields[columnIndex - 1].getName(), new
ValueWrapper(new Float(x)) );
}


***************
*** 955,962 ****
Driver.debug("updating int " +
fields[columnIndex - 1].getName() + "=" + x);

doingUpdates = !onInsertRow;
- updateValues.put( fields[columnIndex - 1].getName(), new
Integer(x) );

}


--- 952,959 ----
Driver.debug("updating int " +
fields[columnIndex - 1].getName() + "=" + x);

doingUpdates = !onInsertRow;

+ updateValues.put( fields[columnIndex - 1].getName(), new
ValueWrapper(new Integer(x)) );
}


***************
*** 972,979 ****
Driver.debug("updating long " +
fields[columnIndex - 1].getName() + "=" + x);

doingUpdates = !onInsertRow;
- updateValues.put( fields[columnIndex - 1].getName(), new
Long(x) );

}


--- 969,976 ----
Driver.debug("updating long " +
fields[columnIndex - 1].getName() + "=" + x);

doingUpdates = !onInsertRow;

+ updateValues.put( fields[columnIndex - 1].getName(), new
ValueWrapper(new Long(x)) );
}


***************
*** 986,994 ****
}

doingUpdates = !onInsertRow;
- updateValues.put( fields[columnIndex - 1].getName(),
null);

!
}


--- 983,990 ----
}

doingUpdates = !onInsertRow;

! updateValues.put( fields[columnIndex - 1].getName(), new
ValueWrapper(null) ); // ensure value is non-null here
}


***************
*** 1004,1010 ****
Driver.debug("updating object " +
fields[columnIndex - 1].getName() + " = " + x);

doingUpdates = !onInsertRow;
! updateValues.put( fields[columnIndex - 1].getName(), x
);
}


--- 1000,1007 ----
Driver.debug("updating object " +
fields[columnIndex - 1].getName() + " = " + x);

doingUpdates = !onInsertRow;
!
! updateValues.put( fields[columnIndex - 1].getName(), new
ValueWrapper(x) );
}


***************
*** 1152,1158 ****
Iterator iterator =
updateValues.values().iterator();
for (; iterator.hasNext(); i++)
{
! updateStatement.setObject( i +
1, iterator.next() );

}
for ( int j = 0; j < numKeys; j++, i++)
--- 1149,1155 ----
Iterator iterator =
updateValues.values().iterator();
for (; iterator.hasNext(); i++)
{
! updateStatement.setObject( i +
1, ((ValueWrapper) iterator.next()).value() );

}
for ( int j = 0; j < numKeys; j++, i++)
***************
*** 1195,1204 ****
if ( Driver.logDebug )
Driver.debug("in update Short " +
fields[columnIndex - 1].getName() + " = " + x);

-
doingUpdates = !onInsertRow;
- updateValues.put( fields[columnIndex - 1].getName(), new
Short(x) );

}


--- 1192,1200 ----
if ( Driver.logDebug )
Driver.debug("in update Short " +
fields[columnIndex - 1].getName() + " = " + x);

doingUpdates = !onInsertRow;

+ updateValues.put( fields[columnIndex - 1].getName(), new
ValueWrapper(new Short(x)) );
}


***************
*** 1209,1216 ****
Driver.debug("in update String " +
fields[columnIndex - 1].getName() + " = " + x);

doingUpdates = !onInsertRow;
- updateValues.put( fields[columnIndex - 1].getName(), x
);

}


--- 1205,1212 ----
Driver.debug("in update String " +
fields[columnIndex - 1].getName() + " = " + x);

doingUpdates = !onInsertRow;

+ updateValues.put( fields[columnIndex - 1].getName(), new
ValueWrapper(x) );
}


***************
*** 1220,1229 ****
if ( Driver.logDebug )
Driver.debug("in update Time " +
fields[columnIndex - 1].getName() + " = " + x);

-
doingUpdates = !onInsertRow;
- updateValues.put( fields[columnIndex - 1].getName(), x
);

}


--- 1216,1224 ----
if ( Driver.logDebug )
Driver.debug("in update Time " +
fields[columnIndex - 1].getName() + " = " + x);

doingUpdates = !onInsertRow;

+ updateValues.put( fields[columnIndex - 1].getName(), new
ValueWrapper(x) );
}


***************
*** 1234,1242 ****
Driver.debug("updating Timestamp " +
fields[columnIndex - 1].getName() + " = " + x);

doingUpdates = !onInsertRow;
- updateValues.put( fields[columnIndex - 1].getName(), x
);

!
}


--- 1229,1236 ----
Driver.debug("updating Timestamp " +
fields[columnIndex - 1].getName() + " = " + x);

doingUpdates = !onInsertRow;

! updateValues.put( fields[columnIndex - 1].getName(), new
ValueWrapper(x) );
}


***************
*** 1546,1558 ****
case Types.REAL:
case Types.TINYINT:

! rowBuffer[columnIndex] =
connection.getEncoding().encode(String.valueOf( updateValues.get(
columnName ) ));

case Types.NULL:
continue;

default:
! rowBuffer[columnIndex] =
(byte[]) updateValues.get( columnName );
}

}
--- 1540,1552 ----
case Types.REAL:
case Types.TINYINT:

! rowBuffer[columnIndex] =
connection.getEncoding().encode(String.valueOf( ((ValueWrapper)
updateValues.get(columnName)).value() ));

case Types.NULL:
continue;

default:
! rowBuffer[columnIndex] =
(byte[]) ((ValueWrapper) updateValues.get(columnName)).value();
}

}
***************
*** 1580,1585 ****
--- 1574,1599 ----
return getObject(index);
}
};
+
+
+ // Purpose: "Wraps" updateable resultset values to prevent
exceptions caused by null value in 'Hashtable.put()' calls
+ private class ValueWrapper
+ {
+ protected Object value; // value being enclosed
+
+ ValueWrapper(Object value)
+ {
+ this.value = value;
+ }
+ Object value()
+ {
+ return this.value;
+ }
+ boolean isNull()
+ {
+ return this.value == null;
+ }
+ };


Browse pgsql-jdbc by date

  From Date Subject
Next Message Kris Jurka 2002-09-20 05:19:46 Re: JDBC Driver - Schema Awareness
Previous Message Dave Cramer 2002-09-19 18:41:28 Re: Implementing JDBC3 methods (Was: JDBC and fetching