Commons CSV
  1. Commons CSV
  2. CSV-100

CSVParser: getHeaderMap throws NPE

    Details

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

      commons-cvs version is the git-clone of current trunk

      Description

      title nearly says it all

      Given a CSVParser parser, the following line throws an NPE:

      Map<String, Integer> header = parser.getHeaderMap();
      

      Stacktrace:

      Caused by: java.lang.NullPointerException
      at java.util.HashMap.<init>(HashMap.java:318)
      at java.util.LinkedHashMap.<init>(LinkedHashMap.java:212)
      at org.apache.commons.csv.CSVParser.getHeaderMap(CSVParser.java:288)
      

      happens if the format doesn't have a headerMap.

      to fix, check if the parser's headerMap is null before trying to create the returned map:

      public Map<String, Integer> getHeaderMap() {
          return this.headerMap != null ?
             new LinkedHashMap<String, Integer>(this.headerMap)
             : null;
      }
      
      

        Activity

        Hide
        Gary Gregory added a comment -
        commit -m "[CSV-100] CSVParser: getHeaderMap throws NPE." C:/vcs/svn/apache/commons/trunks-proper/csv/src/main/java/org/apache/commons/csv/CSVParser.java C:/vcs/svn/apache/commons/trunks-proper/csv/src/test/java/org/apache/commons/csv/CSVParserTest.java
            Sending        C:/vcs/svn/apache/commons/trunks-proper/csv/src/main/java/org/apache/commons/csv/CSVParser.java
            Sending        C:/vcs/svn/apache/commons/trunks-proper/csv/src/test/java/org/apache/commons/csv/CSVParserTest.java
            Transmitting file data ...
            Committed revision 1524435.
        
        Show
        Gary Gregory added a comment - commit -m "[CSV-100] CSVParser: getHeaderMap throws NPE." C:/vcs/svn/apache/commons/trunks-proper/csv/src/main/java/org/apache/commons/csv/CSVParser.java C:/vcs/svn/apache/commons/trunks-proper/csv/src/test/java/org/apache/commons/csv/CSVParserTest.java Sending C:/vcs/svn/apache/commons/trunks-proper/csv/src/main/java/org/apache/commons/csv/CSVParser.java Sending C:/vcs/svn/apache/commons/trunks-proper/csv/src/test/java/org/apache/commons/csv/CSVParserTest.java Transmitting file data ... Committed revision 1524435.
        Hide
        Gary Gregory added a comment -

        Thank you for the report!

        Gary

        Show
        Gary Gregory added a comment - Thank you for the report! Gary

          People

          • Assignee:
            Gary Gregory
            Reporter:
            Jeanette Winzenburg
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development