how to parse non-string values in Opencsv HeaderColumnNameMappingStrategy












2















I'm using a HeaderColumnNameMappingStrategy to map a csv file with a header into a JavaBean. String values parse fine but any "true" or "false" value in csv doesn't map to JavaBean and I get the following exception from the PropertyDescriptor:



java.lang.IllegalArgumentException: argument type mismatch


The code where it occurs is in CsvToBean, line 64:



protected T processLine(MappingStrategy<T> mapper, String line) throws  
IllegalAccessException, InvocationTargetException, InstantiationException, IntrospectionException {
T bean = mapper.createBean();
for(int col = 0; col < line.length; col++) {
String value = line[col];
PropertyDescriptor prop = mapper.findDescriptor(col);
if (null != prop) {
Object obj = convertValue(value, prop);
// this is where exception is thrown for a "true" value in csv
prop.getWriteMethod().invoke(bean, new Object {obj});
}
}
return bean;
}

protected PropertyEditor getPropertyEditor(PropertyDescriptor desc) throws
InstantiationException, IllegalAccessException {
Class<?> cls = desc.getPropertyEditorClass();
if (null != cls) return (PropertyEditor) cls.newInstance();
return getPropertyEditorValue(desc.getPropertyType());
}


I can confirm (via debugger) that the setter method id correctly retrieved at this point.



The problem occurs in desc.getPropertyEditorClass() since it returns null. I assumed primitive types and its wrappers are supported. Are they not?










share|improve this question





























    2















    I'm using a HeaderColumnNameMappingStrategy to map a csv file with a header into a JavaBean. String values parse fine but any "true" or "false" value in csv doesn't map to JavaBean and I get the following exception from the PropertyDescriptor:



    java.lang.IllegalArgumentException: argument type mismatch


    The code where it occurs is in CsvToBean, line 64:



    protected T processLine(MappingStrategy<T> mapper, String line) throws  
    IllegalAccessException, InvocationTargetException, InstantiationException, IntrospectionException {
    T bean = mapper.createBean();
    for(int col = 0; col < line.length; col++) {
    String value = line[col];
    PropertyDescriptor prop = mapper.findDescriptor(col);
    if (null != prop) {
    Object obj = convertValue(value, prop);
    // this is where exception is thrown for a "true" value in csv
    prop.getWriteMethod().invoke(bean, new Object {obj});
    }
    }
    return bean;
    }

    protected PropertyEditor getPropertyEditor(PropertyDescriptor desc) throws
    InstantiationException, IllegalAccessException {
    Class<?> cls = desc.getPropertyEditorClass();
    if (null != cls) return (PropertyEditor) cls.newInstance();
    return getPropertyEditorValue(desc.getPropertyType());
    }


    I can confirm (via debugger) that the setter method id correctly retrieved at this point.



    The problem occurs in desc.getPropertyEditorClass() since it returns null. I assumed primitive types and its wrappers are supported. Are they not?










    share|improve this question



























      2












      2








      2








      I'm using a HeaderColumnNameMappingStrategy to map a csv file with a header into a JavaBean. String values parse fine but any "true" or "false" value in csv doesn't map to JavaBean and I get the following exception from the PropertyDescriptor:



      java.lang.IllegalArgumentException: argument type mismatch


      The code where it occurs is in CsvToBean, line 64:



      protected T processLine(MappingStrategy<T> mapper, String line) throws  
      IllegalAccessException, InvocationTargetException, InstantiationException, IntrospectionException {
      T bean = mapper.createBean();
      for(int col = 0; col < line.length; col++) {
      String value = line[col];
      PropertyDescriptor prop = mapper.findDescriptor(col);
      if (null != prop) {
      Object obj = convertValue(value, prop);
      // this is where exception is thrown for a "true" value in csv
      prop.getWriteMethod().invoke(bean, new Object {obj});
      }
      }
      return bean;
      }

      protected PropertyEditor getPropertyEditor(PropertyDescriptor desc) throws
      InstantiationException, IllegalAccessException {
      Class<?> cls = desc.getPropertyEditorClass();
      if (null != cls) return (PropertyEditor) cls.newInstance();
      return getPropertyEditorValue(desc.getPropertyType());
      }


      I can confirm (via debugger) that the setter method id correctly retrieved at this point.



      The problem occurs in desc.getPropertyEditorClass() since it returns null. I assumed primitive types and its wrappers are supported. Are they not?










      share|improve this question
















      I'm using a HeaderColumnNameMappingStrategy to map a csv file with a header into a JavaBean. String values parse fine but any "true" or "false" value in csv doesn't map to JavaBean and I get the following exception from the PropertyDescriptor:



      java.lang.IllegalArgumentException: argument type mismatch


      The code where it occurs is in CsvToBean, line 64:



      protected T processLine(MappingStrategy<T> mapper, String line) throws  
      IllegalAccessException, InvocationTargetException, InstantiationException, IntrospectionException {
      T bean = mapper.createBean();
      for(int col = 0; col < line.length; col++) {
      String value = line[col];
      PropertyDescriptor prop = mapper.findDescriptor(col);
      if (null != prop) {
      Object obj = convertValue(value, prop);
      // this is where exception is thrown for a "true" value in csv
      prop.getWriteMethod().invoke(bean, new Object {obj});
      }
      }
      return bean;
      }

      protected PropertyEditor getPropertyEditor(PropertyDescriptor desc) throws
      InstantiationException, IllegalAccessException {
      Class<?> cls = desc.getPropertyEditorClass();
      if (null != cls) return (PropertyEditor) cls.newInstance();
      return getPropertyEditorValue(desc.getPropertyType());
      }


      I can confirm (via debugger) that the setter method id correctly retrieved at this point.



      The problem occurs in desc.getPropertyEditorClass() since it returns null. I assumed primitive types and its wrappers are supported. Are they not?







      javabeans opencsv






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 1 '11 at 15:54







      pri

















      asked Nov 29 '11 at 20:39









      pripri

      94321628




      94321628
























          3 Answers
          3






          active

          oldest

          votes


















          5














          I've run into this same issue. The cleanest way is probably to override getPropertyEditor like pritam did above and return a custom PropertyEditor for your particular object. The quick and dirty way would be to override convertValue in anonymous class form, like this:



          CsvToBean<MyClass> csvToBean = new CsvToBean<MyClass>(){

          @Override
          protected Object convertValue(String value, PropertyDescriptor prop) throws InstantiationException,IllegalAccessException {

          if (prop.getName().equals("myWhatever")) {
          // return an custom object based on the incoming value
          return new MyWhatever((String)value);
          }

          return super.convertValue(value, prop);
          }
          };


          This is working fine for me with OpenCSV 2.3. Good luck!






          share|improve this answer































            1














            I resolved this by extending CsvToBean and adding my own PropertyEditors. Turns out opencsv just supports primitive types and no wrappers.






            share|improve this answer
























            • by any chance if you remember how did you actually create your own property editors?

              – Jatin Sehgal
              May 25 '15 at 13:58











            • Registering your own editor may not work because the BooleanEditor is already register. To register an editor you must call: PropertyEditorManager.registerEditor(Integer.class, IntegerEmptyEditor.class);

              – Remy Mellet
              Nov 15 '15 at 20:00





















            0














            Pritam's answer is great and this is a sample for dealing with datetime format.
            PropertyEditorManager.registerEditor(java.util.Date.class, DateEditor.class);

            You should write your own editor class like this:
            public class DateEditor extends PropertyEditorSupport{
            public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            @Override
            public void setAsText(String text){
            setValue(sdf.parse(text));}
            }






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8317413%2fhow-to-parse-non-string-values-in-opencsv-headercolumnnamemappingstrategy%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              5














              I've run into this same issue. The cleanest way is probably to override getPropertyEditor like pritam did above and return a custom PropertyEditor for your particular object. The quick and dirty way would be to override convertValue in anonymous class form, like this:



              CsvToBean<MyClass> csvToBean = new CsvToBean<MyClass>(){

              @Override
              protected Object convertValue(String value, PropertyDescriptor prop) throws InstantiationException,IllegalAccessException {

              if (prop.getName().equals("myWhatever")) {
              // return an custom object based on the incoming value
              return new MyWhatever((String)value);
              }

              return super.convertValue(value, prop);
              }
              };


              This is working fine for me with OpenCSV 2.3. Good luck!






              share|improve this answer




























                5














                I've run into this same issue. The cleanest way is probably to override getPropertyEditor like pritam did above and return a custom PropertyEditor for your particular object. The quick and dirty way would be to override convertValue in anonymous class form, like this:



                CsvToBean<MyClass> csvToBean = new CsvToBean<MyClass>(){

                @Override
                protected Object convertValue(String value, PropertyDescriptor prop) throws InstantiationException,IllegalAccessException {

                if (prop.getName().equals("myWhatever")) {
                // return an custom object based on the incoming value
                return new MyWhatever((String)value);
                }

                return super.convertValue(value, prop);
                }
                };


                This is working fine for me with OpenCSV 2.3. Good luck!






                share|improve this answer


























                  5












                  5








                  5







                  I've run into this same issue. The cleanest way is probably to override getPropertyEditor like pritam did above and return a custom PropertyEditor for your particular object. The quick and dirty way would be to override convertValue in anonymous class form, like this:



                  CsvToBean<MyClass> csvToBean = new CsvToBean<MyClass>(){

                  @Override
                  protected Object convertValue(String value, PropertyDescriptor prop) throws InstantiationException,IllegalAccessException {

                  if (prop.getName().equals("myWhatever")) {
                  // return an custom object based on the incoming value
                  return new MyWhatever((String)value);
                  }

                  return super.convertValue(value, prop);
                  }
                  };


                  This is working fine for me with OpenCSV 2.3. Good luck!






                  share|improve this answer













                  I've run into this same issue. The cleanest way is probably to override getPropertyEditor like pritam did above and return a custom PropertyEditor for your particular object. The quick and dirty way would be to override convertValue in anonymous class form, like this:



                  CsvToBean<MyClass> csvToBean = new CsvToBean<MyClass>(){

                  @Override
                  protected Object convertValue(String value, PropertyDescriptor prop) throws InstantiationException,IllegalAccessException {

                  if (prop.getName().equals("myWhatever")) {
                  // return an custom object based on the incoming value
                  return new MyWhatever((String)value);
                  }

                  return super.convertValue(value, prop);
                  }
                  };


                  This is working fine for me with OpenCSV 2.3. Good luck!







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 13 '12 at 21:09









                  Nathan BeachNathan Beach

                  1,64711823




                  1,64711823

























                      1














                      I resolved this by extending CsvToBean and adding my own PropertyEditors. Turns out opencsv just supports primitive types and no wrappers.






                      share|improve this answer
























                      • by any chance if you remember how did you actually create your own property editors?

                        – Jatin Sehgal
                        May 25 '15 at 13:58











                      • Registering your own editor may not work because the BooleanEditor is already register. To register an editor you must call: PropertyEditorManager.registerEditor(Integer.class, IntegerEmptyEditor.class);

                        – Remy Mellet
                        Nov 15 '15 at 20:00


















                      1














                      I resolved this by extending CsvToBean and adding my own PropertyEditors. Turns out opencsv just supports primitive types and no wrappers.






                      share|improve this answer
























                      • by any chance if you remember how did you actually create your own property editors?

                        – Jatin Sehgal
                        May 25 '15 at 13:58











                      • Registering your own editor may not work because the BooleanEditor is already register. To register an editor you must call: PropertyEditorManager.registerEditor(Integer.class, IntegerEmptyEditor.class);

                        – Remy Mellet
                        Nov 15 '15 at 20:00
















                      1












                      1








                      1







                      I resolved this by extending CsvToBean and adding my own PropertyEditors. Turns out opencsv just supports primitive types and no wrappers.






                      share|improve this answer













                      I resolved this by extending CsvToBean and adding my own PropertyEditors. Turns out opencsv just supports primitive types and no wrappers.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Dec 1 '11 at 17:10









                      pripri

                      94321628




                      94321628













                      • by any chance if you remember how did you actually create your own property editors?

                        – Jatin Sehgal
                        May 25 '15 at 13:58











                      • Registering your own editor may not work because the BooleanEditor is already register. To register an editor you must call: PropertyEditorManager.registerEditor(Integer.class, IntegerEmptyEditor.class);

                        – Remy Mellet
                        Nov 15 '15 at 20:00





















                      • by any chance if you remember how did you actually create your own property editors?

                        – Jatin Sehgal
                        May 25 '15 at 13:58











                      • Registering your own editor may not work because the BooleanEditor is already register. To register an editor you must call: PropertyEditorManager.registerEditor(Integer.class, IntegerEmptyEditor.class);

                        – Remy Mellet
                        Nov 15 '15 at 20:00



















                      by any chance if you remember how did you actually create your own property editors?

                      – Jatin Sehgal
                      May 25 '15 at 13:58





                      by any chance if you remember how did you actually create your own property editors?

                      – Jatin Sehgal
                      May 25 '15 at 13:58













                      Registering your own editor may not work because the BooleanEditor is already register. To register an editor you must call: PropertyEditorManager.registerEditor(Integer.class, IntegerEmptyEditor.class);

                      – Remy Mellet
                      Nov 15 '15 at 20:00







                      Registering your own editor may not work because the BooleanEditor is already register. To register an editor you must call: PropertyEditorManager.registerEditor(Integer.class, IntegerEmptyEditor.class);

                      – Remy Mellet
                      Nov 15 '15 at 20:00













                      0














                      Pritam's answer is great and this is a sample for dealing with datetime format.
                      PropertyEditorManager.registerEditor(java.util.Date.class, DateEditor.class);

                      You should write your own editor class like this:
                      public class DateEditor extends PropertyEditorSupport{
                      public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                      @Override
                      public void setAsText(String text){
                      setValue(sdf.parse(text));}
                      }






                      share|improve this answer




























                        0














                        Pritam's answer is great and this is a sample for dealing with datetime format.
                        PropertyEditorManager.registerEditor(java.util.Date.class, DateEditor.class);

                        You should write your own editor class like this:
                        public class DateEditor extends PropertyEditorSupport{
                        public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        @Override
                        public void setAsText(String text){
                        setValue(sdf.parse(text));}
                        }






                        share|improve this answer


























                          0












                          0








                          0







                          Pritam's answer is great and this is a sample for dealing with datetime format.
                          PropertyEditorManager.registerEditor(java.util.Date.class, DateEditor.class);

                          You should write your own editor class like this:
                          public class DateEditor extends PropertyEditorSupport{
                          public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                          @Override
                          public void setAsText(String text){
                          setValue(sdf.parse(text));}
                          }






                          share|improve this answer













                          Pritam's answer is great and this is a sample for dealing with datetime format.
                          PropertyEditorManager.registerEditor(java.util.Date.class, DateEditor.class);

                          You should write your own editor class like this:
                          public class DateEditor extends PropertyEditorSupport{
                          public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                          @Override
                          public void setAsText(String text){
                          setValue(sdf.parse(text));}
                          }







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 28 '18 at 9:33









                          xialedoucaicaixialedoucaicai

                          1




                          1






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8317413%2fhow-to-parse-non-string-values-in-opencsv-headercolumnnamemappingstrategy%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Contact image not getting when fetch all contact list from iPhone by CNContact

                              count number of partitions of a set with n elements into k subsets

                              A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks