导入Excel存在空行,导致NullPointerException

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_27988539/article/details/83065804

1.情景再现:

通过导入Excel文件,导入数据,把这个Excel中的数据处理成一个List,以方便进一步处理,每行数据对应一个List中的元素。但是在处理这个List的时候,报了NullPointerException。

2.排查过程:

下图为该Excel文件的截图,同过ctrl + end,定位到最后Excel文件使用的最后一个单元格,发现其实第三行是一个空行,只是没有数据,但是这一行是存在的,只是肉眼看不出来。所以,我以为我上传的Excel文件只有两行,其实是有三行,存在一个空行。

下图为debug模式调试结果,

导入的Excel在解析之后size=3,说明该Excel文件确实是有三行。

正是由于这个空行的存在,被解析之后,就存在一个为空的map,于是之后的处理中就报出了NullPointerException。

3.原因分析:

导致Excel数据为空的行存在的原因是我使用了delete键来删除单元格中的数据。按delete键删除选中的单元格中的数据,只会将单元格中的数据删除,但是单元格仍然存在。

而采用下图中的方式,则会删除单元格:

使用上图中的删除方式,就不会存在空白行了。

4.bug修复:

遍历一遍List,将其中为空的map删掉就可以了,之后用户再上传有空白行的Excel文件就不会有问题了。

 

 

展开阅读全文

Spring为Struts2依赖注入失败,导致nullpointerexception

06-12

[color=#FF0000]Web.xml如下:[/color]rnrnrnrn shop9rn rn contextConfigLocationrn classpath:beans.xmlrn rn rn org.springframework.web.context.ContextLoaderListenerrn rnrn rn struts2rn org.apache.struts2.dispatcher.FilterDispatcherrn rn rn struts2rn /*rn rn rn rn index.htmlrn index.htmrn index.jsprn rnrn[color=#FF0000]Struts.xml如下:[/color]rnrnrnrnrn rn rn /test.jsprn rn rnrnrn[color=#FF0000]beans.xml如下:[/color]rnrnrnrn rn rn rn rn rn rn rn rn rn rn rn rn rn rn[color=#FF0000] 。。。。。。(因为内容太多这里的省略掉了。)[/color]rnrnrn[color=#FF0000]action如下:[/color]rnrnpackage com.itcast.action.product;rnrnimport javax.annotation.Resource;rnrnimport org.springframework.stereotype.Controller;rnrnimport com.itcast.bean.ProductType;rnimport com.itcast.service.product.ProductTypeService;rnimport com.opensymphony.xwork2.ActionSupport;rnrn@Controller("/control/product/type/list")rnpublic class ProductTypeAction extends ActionSupportrn @Resource(name="productTypeServiceImpl")rn private ProductTypeService productTypeService;rn private ProductType productType;rn public ProductType getProductType() rn return productType;rn rn public void setProductType(ProductType productType) rn this.productType = productType;rn rn public String execute()rn productType = productTypeService.find(ProductType.class, 3);rn return "success";rn rn public ProductTypeService getProductTypeService() rn return productTypeService;rn rn public void setProductTypeService(ProductTypeService productTypeService) rn this.productTypeService = productTypeService;rn rnrnrn出现的异常信息如下:rnrnjava.lang.NullPointerExceptionrn com.itcast.action.product.ProductTypeAction.execute(ProductTypeAction.java:28)rn sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)rn sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)rn java.lang.reflect.Method.invoke(Method.java:585)rn com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)rn com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)rn com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:163)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:249)rn org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:148)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:93)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:128)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:104)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:148)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:128)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)rn org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)rn org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:468)rn org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)rnrnrnnote The full stack trace of the root cause is available in the Apache Tomcat/6.0.24 logs.rnrn[color=#FF0000]用JUNIT测试:[/color]rn @Testrn public void testAject()rn ProductTypeAction pta = (ProductTypeAction)ctx.getBean("/control/product/type/list");rn ProductTypeService pts = pta.getProductTypeService();rn ProductType pt = pts.find(ProductType.class, 1);rn System.out.println(pt.getName());rn rn可以正常执行,并打印出数据。rnrn请问各位,问题出在哪里? 论坛

导入多个excel文件,导致excel占用内存越来越多。

08-24

一次性导入多个excel文件,导致excel占用内存越来越多,找不出原因。rnrnrnGetFileOpenName("选择文件",ls_filepath,ls_filename[])rn//获取文件路径后调用下面的导入脚本rnrn//导入单个文件的脚本rnxl = Create OLEObjectrnli_ret = xl.ConnectToObject("","Excel.Application")rnIF li_ret <> 0 THENrn// messagebox('',li_ret)rn li_ret = xl.ConnectToNewObject("Excel.Application")rn IF li_ret <> 0 thenrn MessageBox('OLE错误','无法建立Excel通道,请确认是否正确安装了Excel!错误号:' + string(li_ret))rn Destroy xlrn returnrn End ifrnend ifrnrnls_path = "文件路径自动获取"rnxl.Application.Workbooks.Open(ls_path) //打开文件rnrnll_cnt = xl.Application.Workbooks.Count //获取当前workbook的个数rnrnxlsheet = xl.Application.Workbooks[1].Worksheets[2]rnrn////////以下是读数据部分, 首先判断有几行数据,前面几个都为空时则数据结束rnstring ls_company,ls_strrnlong ll_row,ll_qh,ll_cs,ll_idrndec4 ld_opsj[20]rnint li_start,i //从第几行开始是真正的数据rnrnli_start = 2 //从第n+1行开始是真正的数据rnll_row = dw_1.rowcount()rndorn//数据读取到数据窗口rnloop while not (ls_company = "" or isnull(ls_company))rnrn//messagebox('',ll_row - 1)rn//xl.displayalerts = FALSE rn//xl.Application.Workbooks[1].close() rnxl.quit()rn//xlsheet.close()rndestroy xlsheetrn//destroy xlbookrn//destroy xlApprndestroy xl 论坛

没有更多推荐了,返回首页