Details
-
Bug
-
Status: Resolved
-
Major
-
Resolution: Invalid
-
1.8.3
-
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)
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())
07. Converter converter = lookup(clazz);
08. if (converter == null)
11. if (log.isTraceEnabled())
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)
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