为什么我的列表不能正确显示
出现的错误:
我的服务器报错:
java.lang.NullPointerException at org.imooc.util.FileUtil.save(FileUtil.java:19) at org.imooc.service.ExcelService.imp(ExcelService.java:29) at org.imooc.servlet.ImportExcelServlet.doPost(ImportExcelServlet.java:38) at javax.servlet.http.HttpServlet.service(HttpServlet.java:650) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1136) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2555) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2544) 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)
相关的代码:
package org.imooc.util; import org.apache.commons.fileupload.FileItem; import java.io.File; public class FileUtil { /* * * 上传文件的保存路径 * */ public static final String SAVE_PATH = "d:/upload/"; /* * * 保存上传的文件 * */ public static String save(FileItem fileItem,String path) throws Exception { String fileName = System.currentTimeMillis() + "_" + fileItem.getName(); fileItem.write(new File(path + fileName)); return fileName; } }
package org.imooc.service; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.imooc.dto.ImportExcelParamDto; import org.imooc.dto.ImportExcelResultDto; import org.imooc.entity.Student; import org.imooc.util.FileUtil; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class ExcelService { public ImportExcelResultDto imp(ImportExcelParamDto dto){ ImportExcelResultDto result = new ImportExcelResultDto(); result.setTitle(dto.getTitle()); List<Student> studentList = new ArrayList<>(); result.setStudentList(studentList); String fileName = null; try { fileName = FileUtil.save(dto.getExcel(),FileUtil.SAVE_PATH); } catch (Exception e) { e.printStackTrace(); result.setMsg("保存上传文件失败"); } if(fileName != null) { //创建workbook,即取到一个Excel表 try { Workbook workbook = WorkbookFactory.create(new File(FileUtil.SAVE_PATH + fileName)); //然后我们取里面的sheet,我们取的是第一个sheet Sheet sheet = workbook.getSheetAt(0); //接下来,我们取行,然后我们看有效的数据最后一行在哪 int rowNum = sheet.getLastRowNum(); for (int i = 1; i <= rowNum; i++) { //取每一行 Row row = sheet.getRow(i); //取每一行里面的单元格 Student student = new Student(); studentList.add(student); student.setName(row.getCell(0).getStringCellValue()); student.setAge((int)row.getCell(1).getNumericCellValue()); student.setTime( row.getCell(2).getDateCellValue()); } } catch (Exception e) { e.printStackTrace(); result.setMsg("解析Excel失败"); } } return result; } }
package org.imooc.servlet; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.imooc.dto.ImportExcelParamDto; import org.imooc.dto.ImportExcelResultDto; import org.imooc.dto.ParamDto; import org.imooc.service.ExcelService; import org.imooc.util.RequestUtil; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.IOException; import java.util.List; public class ImportExcelServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //判断request流中的内容是普通的表单还是文件类型的 if(ServletFileUpload.isMultipartContent(req)){ ParamDto dto = RequestUtil.parseParam(req); ImportExcelParamDto paramDto = new ImportExcelParamDto(); paramDto.setTitle(dto.getParamMap().get("title")); paramDto.setExcel(dto.getFileMap().get("excle")); ExcelService service = new ExcelService(); ImportExcelResultDto resultDto = service.imp(paramDto); req.setAttribute("result",resultDto); }else{ } req.getRequestDispatcher("WEB-INF/jsp/importExcelResult.jsp").forward(req,resp); } }
0
收起
正在回答
2回答
经测试,在ImportExcelServlet类中,你的excle应该改为excel,因为名字单词写错了,所以获取的excel为null,修订之后的代码如下图,祝学习愉快~
chrismorgen
2018-09-06 12:03:14
FileUtil类中的第19行代码出现空指针制针异常,同学的代码是根据老师的视频一起敲的么?
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星