BeanUtils把 MAP 的值 给Bean ,报 LOG 空指针错。

小杨阿哥哥 发布于 2010/11/15 10:46
阅读 1K+
收藏 0

public static void main(String[] args) {
		User user = new User();
		user.setUserId(33);
		user.setUserName("xiaom");
		user.setUserPwd("mmm");
		user.setRealName("小明");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userName", "cacaca");
		map.put("userId", 55);
		map.put("userPwd", "xxx");
		map.put("realName", "ddd");

		try {
			String userString = JSONUtil.serialize(user);
			System.out.println(userString);

			String mapString = JSONUtil.serialize(map);
			System.out.println(mapString);

			Object o = JSONUtil.deserialize(mapString);
			
			System.out.println(o.getClass());
			System.out.println(o);
			
			BeanUtils.populate(user,map );
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

报错如下:

{"realName":"小明","status":0,"userId":33,"userName":"xiaom","userPwd":"mmm"}
{"userPwd":"xxx","userId":55,"userName":"cacaca","realName":"ddd"}
class java.util.HashMap
{userId=55, userPwd=xxx, userName=cacaca, realName=ddd}
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.aoke.cgms.action.UserAction.main(UserAction.java:94)
Caused by: com.sun.org.apache.commons.logging.LogConfigurationException: com.sun.org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException) (Caused by com.sun.org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException))
    at com.sun.org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:543)
    at com.sun.org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235)
    at com.sun.org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:209)
    at com.sun.org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351)
    at com.sun.org.apache.commons.beanutils.BeanUtils.<clinit>(BeanUtils.java:111)
    ... 1 more
Caused by: com.sun.org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException)
    at com.sun.org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:397)
    at com.sun.org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:529)
    ... 5 more
Caused by: java.lang.NullPointerException
    at com.sun.org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:374)
    ... 6 more

加载中
0
红薯
红薯

这段代码本身应该是没错误的,看那个异常是log的配置异常 LogConfigurationException

0
小杨阿哥哥
小杨阿哥哥

哦。我用的是JSONUtils是Struts2 的莫非只能启动起来用。

0
GeneGuo
GeneGuo

看你的 main 方法,应该是在 UserAction 里面,action 肯定不能直接这个用 main 方法测试啊。

0
osrchina
osrchina

发现,这个bean 必须是public的,如果是一个内部类也是不行的

返回顶部
顶部