出现个空指针异常
来源:6-2 项目作业
Mr__Gao
2019-09-01 11:52:19
package com.imooc.web.servlet;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 com.imooc.mainclass.Book;
import com.imooc.utils.UploadUtils;
@WebServlet("/addBookServlet")
public class addBookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public addBookServlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String,String> map=new HashMap<String,String>();
//创建磁盘文件项工厂
DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory();
//创建核心解析类
ServletFileUpload fileUpload=new ServletFileUpload(diskFileItemFactory);
//解析请求对象,将请求分成几个部分FileItem
try {
List<FileItem> list=fileUpload.parseRequest(request);
//遍历集合,获得各个部分的对象
for(FileItem fileItem:list) {
if(fileItem.isFormField()) {
String name=fileItem.getName();
String value=fileItem.getString("UTF-8");
map.put(name, value);
}else {
String fileName=fileItem.getName();
String uuidFileName=UploadUtils.getUuidFileName(fileName);
InputStream is=fileItem.getInputStream();
String path=getServletContext().getRealPath("/upload");
String url= path+"\\"+uuidFileName;
map.put("path", request.getContextPath()+"/upload"+uuidFileName);
OutputStream os=new FileOutputStream(url);
int len=0;
byte[] b=new byte[1024];
while((len=is.read(b))!=-1) {
os.write(b, 0, len);
}
is.close();
os.close();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Book book=new Book();
book.setBookId(map.get("bookId"));
book.setBookName(map.get("bookName"));
book.setCategoryBook(map.get("categoryId"));
book.setPrice(Float.parseFloat(map.get("bookPrice")));
book.setDescription(map.get("remarks"));
book.setPath(map.get("path"));
response.sendRedirect(request.getContextPath()+"/bookList.jsp");
}
}显示错误在book.setPrice(Float.parseFloat(map.get("bookPrice")));
我设置的price是float
3回答
好帮手慕柯南
2019-09-01
同学你好!
获取非文件项的name时,应该使用fileItem.getFieldName();同学这里使用了fileItem.getName()

如果我都回答解决了你的疑惑,请采纳,祝学习愉快~
Mr__Gao
提问者
2019-09-01
我输出了一下map,结果显示的是
在map中,value的值存在,key全是null
Mr__Gao
提问者
2019-09-01
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String loginUser=(String)request.getSession().getAttribute("loginUser");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>新建图书信息</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/add.css">
</head>
<body>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/dept/list.do">
图书信息管理
</a>
</div>
</div>
</nav>
<div class="container">
<div class="jumbotron">
<h1>Hello, <%=loginUser%>!</h1>
<p>请小心地新增图书信息,要是建了一个错误的就不好了。。。</p>
</div>
<div class="page-header">
<h3><small>新建</small></h3>
</div>
<form class="form-horizontal" action="${pageContext.request.contextPath}/addBookServlet" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">图书编号 :</label>
<div class="col-sm-8">
<input name="bookId" class="form-control" id="bookId">
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">图书名称 :</label>
<div class="col-sm-8">
<input name="bookName" class="form-control" id="bookName">
</div>
</div>
<div class="form-group">
<label for="categoryId" class="col-sm-2 control-label">分类 :</label>
<select id="categoryId" name="categoryId" class="col-sm-2 form-control" style="width: auto;margin-left: 15px">
<c:forEach items="${sessionScope.categoryList}" var="cList" varStatus="idx">
<option value="${cList.categoryId}" selected="">${cList.categoryName}</option>
</c:forEach>
<!-- 下拉列表的内容要从分类中进行读取,value值是分类id -->
</select>
</div>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">价格 :</label>
<div class="col-sm-8">
<input name="bookPrice" class="form-control" id="bookPrice">
</div>
</div>
<div class="form-group" >
<label for="name" class="col-sm-2 control-label">图书封面 :</label>
<input type="file" id="bookPic" name="bookPic" style="padding-left: 15px">
</div>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">备注 :</label>
<div class="col-sm-8">
<input name="remarks" class="form-control" id="remarks">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">保存</button>
</div>
</div>
</form>
</div>
<footer class="text-center" >
copy@imooc
</footer>
</body>
</html>这是addbook。jsp
我得到的全是空的数值,在普通项中。id,name什么的在控制台输出的map.get(“”);都是空
相似问题