From a478c716e0065f77b1d6206b24c1d34a7deb7823 Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Wed, 22 Jan 2014 02:49:14 +0000 Subject: [PATCH] Fix NullPointerException in RowImpl.toString when value is null. git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@844 f203690c-595d-4dc9-a70b-905162fa7fd2 --- src/changes/changes.xml | 5 +++++ .../jackcess/impl/CustomToStringStyle.java | 17 +++++++++++++---- .../jackcess/DatabaseTest.java | 8 ++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 40c067a..ca8ed97 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -4,6 +4,11 @@ Tim McCune + + + Fix NullPointerException in RowImpl.toString() when value is null. + + Rework row add/update so that constraint violations do not leave diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/CustomToStringStyle.java b/src/main/java/com/healthmarketscience/jackcess/impl/CustomToStringStyle.java index 437f176..f477241 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/CustomToStringStyle.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/CustomToStringStyle.java @@ -119,11 +119,11 @@ public class CustomToStringStyle extends StandardToStringStyle if(isFieldSeparatorAtStart()) { appendFieldSeparator(sb); } - appendInternal(sb, fieldName, iter.next(), true); + appendValueDetail(sb, fieldName, iter.next()); } while(iter.hasNext()) { sb.append(getArraySeparator()); - appendInternal(sb, fieldName, iter.next(), true); + appendValueDetail(sb, fieldName, iter.next()); } // indent entire list contents another level @@ -151,13 +151,13 @@ public class CustomToStringStyle extends StandardToStringStyle } Map.Entry e = iter.next(); sb.append(e.getKey()).append("="); - appendInternal(sb, fieldName, e.getValue(), true); + appendValueDetail(sb, fieldName, e.getValue()); } while(iter.hasNext()) { sb.append(getArraySeparator()); Map.Entry e = iter.next(); sb.append(e.getKey()).append("="); - appendInternal(sb, fieldName, e.getValue(), true); + appendValueDetail(sb, fieldName, e.getValue()); } // indent entire map contents another level @@ -175,6 +175,15 @@ public class CustomToStringStyle extends StandardToStringStyle appendDetail(buffer, PageChannel.wrap(array)); } + private void appendValueDetail(StringBuffer buffer, String fieldName, + Object value) { + if (value == null) { + appendNullText(buffer, fieldName); + } else { + appendInternal(buffer, fieldName, value, true); + } + } + private static void appendDetail(StringBuffer buffer, ByteBuffer bb) { int len = bb.remaining(); buffer.append("(").append(len).append(") "); diff --git a/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java b/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java index 5d67463..4f2d2c7 100644 --- a/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java +++ b/src/test/java/com/healthmarketscience/jackcess/DatabaseTest.java @@ -1440,6 +1440,14 @@ public class DatabaseTest extends TestCase } } + public void testToString() + { + RowImpl row = new RowImpl(new RowIdImpl(1, 1)); + row.put("id", 37); + row.put("data", null); + assertEquals("Row[1:1][{id=37,data=}]", row.toString()); + } + private void checkRawValue(String expected, Object val) { if(expected != null) { -- 2.39.5