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
This commit is contained in:
parent
fafacd179d
commit
e80b8112a7
|
@ -97,7 +97,7 @@ public class CSVPrinter {
|
||||||
* @param comment the comment to output
|
* @param comment the comment to output
|
||||||
*/
|
*/
|
||||||
public void printComment(String comment) throws IOException {
|
public void printComment(String comment) throws IOException {
|
||||||
if (this.format.isCommentingDisabled()) {
|
if (format.isCommentingDisabled()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!newLine) {
|
if (!newLine) {
|
||||||
|
@ -127,13 +127,7 @@ public class CSVPrinter {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void print(char[] value, int offset, int len, boolean checkForEscape) throws IOException {
|
private void print(char[] value, int offset, int len) throws IOException {
|
||||||
if (!checkForEscape) {
|
|
||||||
printSep();
|
|
||||||
out.write(value, offset, len);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (format.isEncapsulating()) {
|
if (format.isEncapsulating()) {
|
||||||
printAndEncapsulate(value, offset, len);
|
printAndEncapsulate(value, offset, len);
|
||||||
} else if (format.isEscaping()) {
|
} else if (format.isEscaping()) {
|
||||||
|
@ -148,7 +142,7 @@ public class CSVPrinter {
|
||||||
if (newLine) {
|
if (newLine) {
|
||||||
newLine = false;
|
newLine = false;
|
||||||
} else {
|
} else {
|
||||||
out.write(this.format.getDelimiter());
|
out.write(format.getDelimiter());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,8 +153,8 @@ public class CSVPrinter {
|
||||||
|
|
||||||
printSep();
|
printSep();
|
||||||
|
|
||||||
char delim = this.format.getDelimiter();
|
char delim = format.getDelimiter();
|
||||||
char escape = this.format.getEscape();
|
char escape = format.getEscape();
|
||||||
|
|
||||||
while (pos < end) {
|
while (pos < end) {
|
||||||
char c = value[pos];
|
char c = value[pos];
|
||||||
|
@ -201,8 +195,8 @@ public class CSVPrinter {
|
||||||
|
|
||||||
printSep();
|
printSep();
|
||||||
|
|
||||||
char delim = this.format.getDelimiter();
|
char delim = format.getDelimiter();
|
||||||
char encapsulator = this.format.getEncapsulator();
|
char encapsulator = format.getEncapsulator();
|
||||||
|
|
||||||
if (len <= 0) {
|
if (len <= 0) {
|
||||||
// always quote an empty token that is the first
|
// always quote an empty token that is the first
|
||||||
|
@ -288,6 +282,11 @@ public class CSVPrinter {
|
||||||
* @param value value to be outputted.
|
* @param value value to be outputted.
|
||||||
*/
|
*/
|
||||||
public void print(String value, boolean checkForEscape) throws IOException {
|
public void print(String value, boolean checkForEscape) throws IOException {
|
||||||
|
if (value == null) {
|
||||||
|
// null values are considered empty
|
||||||
|
value = "";
|
||||||
|
}
|
||||||
|
|
||||||
if (!checkForEscape) {
|
if (!checkForEscape) {
|
||||||
// write directly from string
|
// write directly from string
|
||||||
printSep();
|
printSep();
|
||||||
|
@ -300,7 +299,7 @@ public class CSVPrinter {
|
||||||
}
|
}
|
||||||
|
|
||||||
value.getChars(0, value.length(), buf, 0);
|
value.getChars(0, value.length(), buf, 0);
|
||||||
print(buf, 0, value.length(), checkForEscape);
|
print(buf, 0, value.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -92,6 +92,13 @@ public class CSVPrinterTest extends TestCase {
|
||||||
assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
|
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 {
|
public void testDisabledComment() throws IOException {
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
||||||
|
|
Loading…
Reference in New Issue