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

        Jeanette Winzenburg created issue -
        Jeanette Winzenburg made changes -
        Field Original Value New Value
        Description title nearly says it all :-)

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

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

        Stacktrace:

        {noformat}
        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)
        {noformat}

        happens if the format doesn't have a headerMap.


        title nearly says it all :-)

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

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

        Stacktrace:

        {noformat}
        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)
        {noformat}

        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:

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

        {code}
        Gary Gregory made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Assignee Gary Gregory [ garydgregory ]
        Fix Version/s 1.0 [ 12320161 ]
        Resolution Fixed [ 1 ]
        Benedikt Ritter made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

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

            Dates

            • Created:
              Updated:
              Resolved:

              Development