[ 
https://issues.apache.org/jira/browse/BEANUTILS-414?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13585122#comment-13585122
 ] 

Benedikt Ritter commented on BEANUTILS-414:
-------------------------------------------

Hi Ken,

sorry I don't know chinese :) Can you please comment the above snippet in 
english? It is hard to read your code example because it contains line numbers 
and is not well formatted. You can use the code makro from jira to format 
source code (see jira formatting [1]).

Going back to your code snipped, it looks like you are trying to call 
{code}
ConvertUtils.convert("1", null)
{code}

This results in a NullPointerException, because you didn't pass the target type 
for the conversion. If you don't want to pass a target type, you can use:

{code}
ConvertUtils.convert(Object)
{code}

This will convert your input to String.

Nevertheless I will add some more JavaDoc so that is become more clear that 
null must not be passed to convert(String, Class).

[1] 
https://issues.apache.org/jira/secure/WikiRendererHelpAction.jspa?section=texteffects
                
> 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
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to