Skip to content

Commit e80b811

Browse files
committed
Fixed the NullPointerException on null values in CSVPrinter and print an empty value instead (SANDBOX-209)
git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1297306 13f79535-47bb-0310-9956-ffa450edef68
1 parent fafacd1 commit e80b811

2 files changed

Lines changed: 20 additions & 14 deletions

File tree

src/main/java/org/apache/commons/csv/CSVPrinter.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void println(String... values) throws IOException {
9797
* @param comment the comment to output
9898
*/
9999
public void printComment(String comment) throws IOException {
100-
if (this.format.isCommentingDisabled()) {
100+
if (format.isCommentingDisabled()) {
101101
return;
102102
}
103103
if (!newLine) {
@@ -127,13 +127,7 @@ public void printComment(String comment) throws IOException {
127127
}
128128

129129

130-
private void print(char[] value, int offset, int len, boolean checkForEscape) throws IOException {
131-
if (!checkForEscape) {
132-
printSep();
133-
out.write(value, offset, len);
134-
return;
135-
}
136-
130+
private void print(char[] value, int offset, int len) throws IOException {
137131
if (format.isEncapsulating()) {
138132
printAndEncapsulate(value, offset, len);
139133
} else if (format.isEscaping()) {
@@ -148,7 +142,7 @@ void printSep() throws IOException {
148142
if (newLine) {
149143
newLine = false;
150144
} else {
151-
out.write(this.format.getDelimiter());
145+
out.write(format.getDelimiter());
152146
}
153147
}
154148

@@ -159,8 +153,8 @@ void printAndEscape(char[] value, int offset, int len) throws IOException {
159153

160154
printSep();
161155

162-
char delim = this.format.getDelimiter();
163-
char escape = this.format.getEscape();
156+
char delim = format.getDelimiter();
157+
char escape = format.getEscape();
164158

165159
while (pos < end) {
166160
char c = value[pos];
@@ -201,8 +195,8 @@ void printAndEncapsulate(char[] value, int offset, int len) throws IOException {
201195

202196
printSep();
203197

204-
char delim = this.format.getDelimiter();
205-
char encapsulator = this.format.getEncapsulator();
198+
char delim = format.getDelimiter();
199+
char encapsulator = format.getEncapsulator();
206200

207201
if (len <= 0) {
208202
// always quote an empty token that is the first
@@ -288,6 +282,11 @@ void printAndEncapsulate(char[] value, int offset, int len) throws IOException {
288282
* @param value value to be outputted.
289283
*/
290284
public void print(String value, boolean checkForEscape) throws IOException {
285+
if (value == null) {
286+
// null values are considered empty
287+
value = "";
288+
}
289+
291290
if (!checkForEscape) {
292291
// write directly from string
293292
printSep();
@@ -300,7 +299,7 @@ public void print(String value, boolean checkForEscape) throws IOException {
300299
}
301300

302301
value.getChars(0, value.length(), buf, 0);
303-
print(buf, 0, value.length(), checkForEscape);
302+
print(buf, 0, value.length());
304303
}
305304

306305
/**

src/test/java/org/apache/commons/csv/CSVPrinterTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ public void testExcelPrinter2() throws IOException {
9292
assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
9393
}
9494

95+
public void testPrintNullValues() throws IOException {
96+
StringWriter sw = new StringWriter();
97+
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
98+
printer.println("a", null, "b");
99+
assertEquals("a,,b" + lineSeparator, sw.toString());
100+
}
101+
95102
public void testDisabledComment() throws IOException {
96103
StringWriter sw = new StringWriter();
97104
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);

0 commit comments

Comments
 (0)