遇到的错误之“Could not copy property 'repositoryId' from source to target; nested exception is java.lang.reflect.InvocationTargetException”
[ERROR][2023-09-03 21:54:10] | Combination收入费用数据导出异常 org.springframework.beans.FatalBeanException: Could not copy property 'repositoryId' from source to target; nested exception is java.lang.reflect.InvocationTargetException at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:626) at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:538) at com.bessky.financial.web.controller.analyze.CombinationIncomeCostController.dealSkuRefundExport(CombinationIncomeCostController.java:2026) at com.bessky.financial.web.controller.analyze.CombinationIncomeCostController.apportionExportData(CombinationIncomeCostController.java:1552) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:854) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:765) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) at javax.servlet.http.HttpServlet.service(HttpServlet.java:655) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:232) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:167) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:194) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:167) at com.bessky.financial.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:80) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:194) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:167) at com.bessky.financial.web.filter.SecurityFilter.doFilter(SecurityFilter.java:145) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:194) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:167) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:194) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:167) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:544) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:698) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:364) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:624) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:831) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1651) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.GeneratedMethodAccessor820.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:622) ... 53 more Caused by: java.lang.NullPointerException at com.bessky.financial.pojo.analyze.SkuRefundAnalysisIncomeDto.setRepositoryId(SkuRefundAnalysisIncomeDto.java:676) ... 57 more
一、异常问题
Could not copy property 'repositoryId' from source to target; nested exception is java.lang.reflect.InvocationTargetException
二、原因
因为针对repositoryId字段的set是这样的,如果传入的值为null就会在报错,因为repositoryId为null
public void setRepositoryId(Integer repositoryId) { this.repositoryId = repositoryId; if (0 == repositoryId) { this.repository = "国内"; } else if (1 == repositoryId) { this.repository = "转运"; } else if (2 == repositoryId) { this.repository = "海外"; } else if (3 == repositoryId) { this.repository = "FBA"; } else if (4 == repositoryId) { this.repository = "分销"; } }
三、解决方案
1.不传入null值
2.优化set方案
四、案例
@Test public void wzwStream() { User user = new User(); User user1 = new User(); user1.setUserId(2); user1.setCreatedBy(2); user1.setLastUpdatedBy(2); user1.setMorePermission(2); user1.setJob(2); user1.setAuthTransition(2); user1.setAuthPerson(2); user1.setDepartmentId(null); BeanUtils.copyProperties(user1, user); System.out.println("user1 = " + user1); }
public void setDepartmentId(Integer departmentId) { this.departmentId = departmentId; if (0 == departmentId) { this.departmentName = "国内"; } else if (1 == departmentId) { this.departmentName = "转运"; } else if (2 == departmentId) { this.departmentName = "海外"; } else if (3 == departmentId) { this.departmentName = "FBA"; } else if (4 == departmentId) { this.departmentName = "分销"; } }
* 博客文章部分截图及内容来自于学习的书本及相应培训课程,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。
* 备注:王子威
* 我的网易邮箱:wzw_1314_520@163.com