Author: ebourg
Date: Tue Mar 6 00:09:51 2012
New Revision: 1297306
URL: http://svn.apache.org/viewvc?rev=1297306&view=rev
Log:
Fixed the NullPointerException on null values in CSVPrinter and print an empty
value instead (SANDBOX-209)
Modified:
commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java
commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
Modified:
commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java?rev=1297306&r1=1297305&r2=1297306&view=diff
==============================================================================
---
commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java
(original)
+++
commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java
Tue Mar 6 00:09:51 2012
@@ -97,7 +97,7 @@ public class CSVPrinter {
* @param comment the comment to output
*/
public void printComment(String comment) throws IOException {
- if (this.format.isCommentingDisabled()) {
+ if (format.isCommentingDisabled()) {
return;
}
if (!newLine) {
@@ -127,13 +127,7 @@ public class CSVPrinter {
}
- private void print(char[] value, int offset, int len, boolean
checkForEscape) throws IOException {
- if (!checkForEscape) {
- printSep();
- out.write(value, offset, len);
- return;
- }
-
+ private void print(char[] value, int offset, int len) throws IOException {
if (format.isEncapsulating()) {
printAndEncapsulate(value, offset, len);
} else if (format.isEscaping()) {
@@ -148,7 +142,7 @@ public class CSVPrinter {
if (newLine) {
newLine = false;
} else {
- out.write(this.format.getDelimiter());
+ out.write(format.getDelimiter());
}
}
@@ -159,8 +153,8 @@ public class CSVPrinter {
printSep();
- char delim = this.format.getDelimiter();
- char escape = this.format.getEscape();
+ char delim = format.getDelimiter();
+ char escape = format.getEscape();
while (pos < end) {
char c = value[pos];
@@ -201,8 +195,8 @@ public class CSVPrinter {
printSep();
- char delim = this.format.getDelimiter();
- char encapsulator = this.format.getEncapsulator();
+ char delim = format.getDelimiter();
+ char encapsulator = format.getEncapsulator();
if (len <= 0) {
// always quote an empty token that is the first
@@ -288,6 +282,11 @@ public class CSVPrinter {
* @param value value to be outputted.
*/
public void print(String value, boolean checkForEscape) throws IOException
{
+ if (value == null) {
+ // null values are considered empty
+ value = "";
+ }
+
if (!checkForEscape) {
// write directly from string
printSep();
@@ -300,7 +299,7 @@ public class CSVPrinter {
}
value.getChars(0, value.length(), buf, 0);
- print(buf, 0, value.length(), checkForEscape);
+ print(buf, 0, value.length());
}
/**
Modified:
commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java?rev=1297306&r1=1297305&r2=1297306&view=diff
==============================================================================
---
commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
(original)
+++
commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
Tue Mar 6 00:09:51 2012
@@ -92,6 +92,13 @@ public class CSVPrinterTest extends Test
assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
}
+ public void testPrintNullValues() throws IOException {
+ StringWriter sw = new StringWriter();
+ CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
+ printer.println("a", null, "b");
+ assertEquals("a,,b" + lineSeparator, sw.toString());
+ }
+
public void testDisabledComment() throws IOException {
StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);