[jira] [Created] (BEANUTILS-414) Object org.apache.commons.beanutils.ConvertUtilsBean.convert(String value, Class clazz) nullPointException

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[jira] [Created] (BEANUTILS-414) Object org.apache.commons.beanutils.ConvertUtilsBean.convert(String value, Class clazz) nullPointException

Michael Osipov (Jira)
Ken.xu created BEANUTILS-414:
--------------------------------

             Summary: Object org.apache.commons.beanutils.ConvertUtilsBean.convert(String value, Class clazz)  nullPointException
                 Key: BEANUTILS-414
                 URL: https://issues.apache.org/jira/browse/BEANUTILS-414
             Project: Commons BeanUtils
          Issue Type: Bug
          Components: ConvertUtils & Converters
    Affects Versions: 1.8.3
         Environment: Java Eclipse
log4j.jar
            Reporter: Ken.xu


 在工程做如下类似使用时,系统会报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  

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira