Commons CSV
  1. Commons CSV
  2. CSV-13

NullPointerException in CSVPrinter.print()/println()

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.0
    • Component/s: Printer
    • Labels:
      None

      Description

      If
      public void print(String value) method of CSVPrinter class gets null parameter it throws NullPointerException.

        Issue Links

          Activity

          Hide
          Emmanuel Bourg added a comment -

          I changed CSVPrinter.print() to handle null values as empty strings. I also removed the print method in CSVUtils that was writing the "null" string on null values.

          Show
          Emmanuel Bourg added a comment - I changed CSVPrinter.print() to handle null values as empty strings. I also removed the print method in CSVUtils that was writing the "null" string on null values.
          Hide
          Emmanuel Bourg added a comment -

          I found the null handling a bit dubious as well. Turning null values into empty strings makes sense. A format parameter can still be introduced later to transform the empty strings into null when parsing.

          Show
          Emmanuel Bourg added a comment - I found the null handling a bit dubious as well. Turning null values into empty strings makes sense. A format parameter can still be introduced later to transform the empty strings into null when parsing.
          Hide
          Henri Yandell added a comment -

          Need to get this right before a 1.0. Great comments from Trejkaz and the direction sounds worthwhile.

          Show
          Henri Yandell added a comment - Need to get this right before a 1.0. Great comments from Trejkaz and the direction sounds worthwhile.
          Hide
          Trejkaz added a comment -

          The following methods should throw NullPointerException or IllegalArgumentException if the parameter itself is null, depending on your preference:
          public void println(String[] values) {
          public void println(String[][] values) {
          public void printlnComment(String comment) {

          The following two methods should behave similarly with null values:
          public void println(String value) {
          public void print(String value) {

          (well, I guess println(String) just does print(String) followed by println()...)

          I think leaving it completely empty is okay. So for instance...
          printer.println(new String[]

          { "1", null, "3" }

          );

          This would result in:
          1,,3

          (I'll use 1,,3 and 1,"",3 to describe the two possible forms from here on.)

          The issue comes down to how to distinguish null from "" when reading CSV back in. Many people have this problem with CSV in other systems and there is no good solution. Some applications say that 1,,3 is a null and 1,"",3 is "" but this isn't standardised at all.

          You can test spreadsheet app behaviour by entering a single quote by itself to create a proper empty string. Excel's behaviour is to export both as 1,,3. OpenOffice Calc appears to assume that a single quote means a string which is the single quote (a bug, probably.)

          So IMO, leaving both as empty is acceptable, and at least consistent with apps which do distinguish between the two internally even if they don't export them in different ways.

          Later, a new CSVStrategy parameter might be introduced, something like get/setQuoteEmptyStrings – assuming users actually need to distinguish between the two. Personally when reading from a CSV file I use StringUtils.isEmpty anyway, so whether it's null or "" doesn't matter to me.

          As for ignoreEmptyLines, it's my belief that this should only apply to truly empty lines. If users insist on having a CSV file with a single column then yes, there is a risk of this single column being null and thus empty, and thus generating a blank line. On the other hand, I don't think many users would be using a CSV library if their data is only in a single column – and two column data wouldn't have this issue as they should at the very least have a single comma even if both cells are blank for a given row (both Excel and OpenOffice.org do it this way.)

          Show
          Trejkaz added a comment - The following methods should throw NullPointerException or IllegalArgumentException if the parameter itself is null, depending on your preference: public void println(String[] values) { public void println(String[][] values) { public void printlnComment(String comment) { The following two methods should behave similarly with null values: public void println(String value) { public void print(String value) { (well, I guess println(String) just does print(String) followed by println()...) I think leaving it completely empty is okay. So for instance... printer.println(new String[] { "1", null, "3" } ); This would result in: 1,,3 (I'll use 1,,3 and 1,"",3 to describe the two possible forms from here on.) The issue comes down to how to distinguish null from "" when reading CSV back in. Many people have this problem with CSV in other systems and there is no good solution. Some applications say that 1,,3 is a null and 1,"",3 is "" but this isn't standardised at all. You can test spreadsheet app behaviour by entering a single quote by itself to create a proper empty string. Excel's behaviour is to export both as 1,,3. OpenOffice Calc appears to assume that a single quote means a string which is the single quote (a bug, probably.) So IMO, leaving both as empty is acceptable, and at least consistent with apps which do distinguish between the two internally even if they don't export them in different ways. Later, a new CSVStrategy parameter might be introduced, something like get/setQuoteEmptyStrings – assuming users actually need to distinguish between the two. Personally when reading from a CSV file I use StringUtils.isEmpty anyway, so whether it's null or "" doesn't matter to me. As for ignoreEmptyLines, it's my belief that this should only apply to truly empty lines. If users insist on having a CSV file with a single column then yes, there is a risk of this single column being null and thus empty, and thus generating a blank line. On the other hand, I don't think many users would be using a CSV library if their data is only in a single column – and two column data wouldn't have this issue as they should at the very least have a single comma even if both cells are blank for a given row (both Excel and OpenOffice.org do it this way.)
          Hide
          Henri Yandell added a comment -

          So the API has:

          public void println(String value) {
          public void println() {
          public void println(String[] values) {
          public void println(String[][] values) {
          public void printlnComment(String comment) {
          public void print(String value) {

          Question being; what should the null functionality be in each case. If you look at either System.out.println or StringWriter, it is to turn 'null' into "null". In the array cases, I guess it would be

          { "null" }

          and {{ "null" }}. Very lame.

          So the other obvious solution would be to implement empty functionality. That still leaves an open question of whether that means nothing happens, or it's akin to an empty string. ie) in the println(null) case, would you expect a newline to be printed.

          I'm thinking that can be up to the CSVStrategy.ignoreEmptyLines - if it's true, then null means nothing. If false then null means an empty string. Or is that bad because it's reusing a configuration option for something that is not necessarily symmetrical with the usage in the parser

          Show
          Henri Yandell added a comment - So the API has: public void println(String value) { public void println() { public void println(String[] values) { public void println(String[][] values) { public void printlnComment(String comment) { public void print(String value) { Question being; what should the null functionality be in each case. If you look at either System.out.println or StringWriter, it is to turn 'null' into "null". In the array cases, I guess it would be { "null" } and {{ "null" }}. Very lame. So the other obvious solution would be to implement empty functionality. That still leaves an open question of whether that means nothing happens, or it's akin to an empty string. ie) in the println(null) case, would you expect a newline to be printed. I'm thinking that can be up to the CSVStrategy.ignoreEmptyLines - if it's true, then null means nothing. If false then null means an empty string. Or is that bad because it's reusing a configuration option for something that is not necessarily symmetrical with the usage in the parser

            People

            • Assignee:
              Unassigned
              Reporter:
              Andriy Sholokh
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development