Uploaded image for project: 'Commons BeanUtils'
  1. Commons BeanUtils
  2. BEANUTILS-414

Object org.apache.commons.beanutils.ConvertUtilsBean.convert(String value, Class clazz) nullPointException

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Invalid
    • 1.8.3
    • 1.9.5
    • None
    • Java Eclipse
      log4j.jar

    Description

      在工程做如下类似使用时,系统会报null指针异常。

      [java] view plaincopyprint?
      01.import org.apache.commons.beanutils.ConvertUtils;
      02.
      03.public class T {
      04.
      05. /**
      06. * @param args
      07. * @author Ken_xu
      08. */
      09. public static void main(String[] args)

      { 10. // TODO Auto-generated method stub 11. Object obj = ConvertUtils.convert("1", null); 12. System.out.println(obj); 13. }


      14.
      15.}
      import org.apache.commons.beanutils.ConvertUtils;

      public class T {

      /**

      • @param args
      • @author Ken_xu
        */
        public static void main(String[] args) { // TODO Auto-generated method stub Object obj = ConvertUtils.convert("1", null); System.out.println(obj); }

      }

      Exception in thread "main" java.lang.NullPointerException
      at org.apache.commons.beanutils.ConvertUtilsBean.convert(ConvertUtilsBean.java:418)
      at org.apache.commons.beanutils.ConvertUtils.convert(ConvertUtils.java:217)
      at T.main(T.java:11)

      经过分析源代码发现

      Object org.apache.commons.beanutils.ConvertUtilsBean.convert(String value, Class clazz)
      这个类的这个方法的源代码如下

      [java] view plaincopyprint?
      01.public Object convert(String value, Class clazz) {
      02.
      03. if (log.isDebugEnabled())

      { 04. log.debug("Convert string '" + value + "' to class '" + 05. clazz.getName() + "'"); 06. }


      07. Converter converter = lookup(clazz);
      08. if (converter == null)

      { 09. converter = lookup(String.class); 10. }


      11. if (log.isTraceEnabled())

      { 12. log.trace(" Using converter " + converter); 13. }


      14. return (converter.convert(clazz, value));
      15.
      16.}
      public Object convert(String value, Class clazz) {

      if (log.isDebugEnabled())

      { log.debug("Convert string '" + value + "' to class '" + clazz.getName() + "'"); }

      Converter converter = lookup(clazz);
      if (converter == null)

      { converter = lookup(String.class); }

      if (log.isTraceEnabled())

      { log.trace(" Using converter " + converter); }

      return (converter.convert(clazz, value));

      }

      分析后认为是由于当前工程使用了log4j实现,并且设置了全局debug级别,最终导致报异常。

      解决方法如下:
      在log4j.properties配置文件中增加如下代码

      [XML] view plaincopyprint?
      01.#屏蔽BUG org.apache.commons.beanutils.ConvertUtilsBean.convert(String value, Class clazz)
      02.log4j.logger.org.apache.commons.beanutils=INFO

      Attachments

        Activity

          People

            britter Benedikt Ritter
            xzknet Ken.xu
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: