为什么会出现空指针异常
@WebServlet(name = "importExcelInitServlet")
public class importExcelServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(ServletFileUpload.isMultipartContent(request)){
ParaDto paraDto = RequestUtil.parseParam(request) ;
ImportExcelParamDto paramDto = new ImportExcelParamDto() ;
paramDto.setTitles(paraDto.getParamMap().get("title"));
paramDto.setExcel(paraDto.getFileMap().get("excel"));
ExcelService service = new ExcelService();
ImportExcelResultDto resultDto = service.imp(paramDto) ;
request.setAttribute("result",resultDto);
} else {
}
request.getRequestDispatcher("/WEB-INF/jsp/importExcelResult.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
public class ExcelService {
public ImportExcelResultDto imp(ImportExcelParamDto paramDto) {
ImportExcelResultDto excelResultDto = new ImportExcelResultDto();
excelResultDto.setTitle(paramDto.getTitles());
List<Student> students = new ArrayList<>();
excelResultDto.setStudents(students);
String fileName = null;
try {
fileName = FileUtil.save(paramDto.getExcel(), FileUtil.SAVE_PATH);
} catch (Exception e) {
e.printStackTrace();
excelResultDto.setMsg("保存上传文件失败了!");
}
Workbook workbook = null;
if (fileName != null) {
try {
//文件传入专门的文件保存类,由文件保存类专门进行保存工作
workbook = WorkbookFactory.create(new File(fileName));
Sheet sheet = workbook.getSheet("Sheet1");
int rowNumber = sheet.getLastRowNum();
for (int i = 1; i <= rowNumber; i++) {
Row row = sheet.getRow(i);
Student student = new Student();
student.setName(row.getCell(0).getStringCellValue());
student.setAge((int) (row.getCell(1).getNumericCellValue()));
students.add(student);
}
} catch (Exception e) {
e.printStackTrace();
excelResultDto.setMsg("解析Excel失败!");
} finally {
if (workbook != null) {
try {
workbook.close();
} catch (IOException e){
e.printStackTrace();
}
}
}
}
return excelResultDto;
}
}
public class FileUtil {
public static final String SAVE_PATH = "d:/uploadFile/" ;
public static String save(FileItem fileItem , String filePath) throws Exception {
String fileName = System.currentTimeMillis() + "_" + fileItem.getName() ; //取得文件名时加上时间戳,防止相同文件名导致错误
fileItem.write(new File(filePath + fileName ));
return fileName ;
}
}
java.lang.NullPointerException
at com.imooc.util.FileUtil.save(FileUtil.java:13)
at com.imooc.service.ExcelService.imp(ExcelService.java:29)
at com.imooc.servlet.importExcelServlet.doPost(importExcelServlet.java:33)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
然后最后解析出来的项目没有显示
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程


恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星