BeanUtils把 MAP 的值 给Bean ,报 LOG 空指针错。
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
发现,这个bean 必须是public的,如果是一个内部类也是不行的
看你的 main 方法,应该是在 UserAction 里面,action 肯定不能直接这个用 main 方法测试啊。
哦。我用的是JSONUtils是Struts2 的莫非只能启动起来用。
这段代码本身应该是没错误的,看那个异常是log的配置异常 LogConfigurationException