开发者社区> 问答> 正文

启动后访问页面报错:java.lang.illegalargumentexcep?报错

@黄勇 你好,想跟你请教个问题:

黄老师,你好。

我将你的书《架构探险》的源代码在eclipse执行,用jdk1.6,建了表customer,当我访问页面时总是报

java.lang.IllegalArgumentException: wrong number of arguments
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	java.lang.reflect.Method.invoke(Method.java:606)
	com.sunny.study.smart.framework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:47)
	com.sunny.study.smart.framework.DispatcherServlet.service(DispatcherServlet.java:82)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

我报错代码是DispatcherServlet.java:

if (param.isEmpty()) {
                    result = ReflectionUtil.invokeMethod(controllerBean, actionMethod);
                } else {
                    result = ReflectionUtil.invokeMethod(controllerBean, actionMethod, param);
                }

调用的方法是ReflectionUtil类中的

public static Object invokeMethod(Object obj, Method method, Object... args) {
        Object result;
        try {
            method.setAccessible(true);
            result = method.invoke(obj, args);
        } catch (Exception e) {
            LOGGER.error("invoke method failure", e);
            throw new RuntimeException(e);
        }
        return result;
    }

这段是因为在DispatcherServlet没有送参数args,但是这不是可变参数吗?应该是可以不送的,看代码应该是没问题的

麻烦有时间请指教!


展开
收起
爱吃鱼的程序员 2020-06-10 15:25:54 651 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    问题解决了,在invokeMethod方法中加个判断

    try{
              if(args.length==0){
                args=newObject[1];
             }
               method.setAccessible(true);
               result=method.invoke(obj,args);
           }

    2020-06-10 15:26:12
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载