空指针异常
FoodDaoImpl.java 改动:在servlet中使用注解,设置了url和启动加载级数。添加了db的get方法
package com.imooc.demo.java; import java.util.ArrayList; import java.util.List; /** * 菜品数据处理类 * @author m1318 * */ public class FoodDaoImpl { //存放菜品对象的全局静态容器List private static final List<Food> foodList = new ArrayList<Food>(); //foodList的get方法 public static List<Food> getFoodlist() { return foodList; } //添加菜品 public void addFood(Food food) { //判断要添加的food对象是否为空 if(food != null) { //food对象不为空,添加到foodList中 foodList.add(food); } } //查询所有菜品信息 public List<Food> getAllFood(){ //返回全局容器foodList List<Food> fl = new ArrayList<Food>(); for(Food food : foodList) { fl.add(food); } return fl; } //根据菜品名称查询菜品信息 public Food getFoodByName(String foodName) { //遍历foodList,以获取每个元素的foodName for(Food food : foodList) { //判断要查找的菜品名是否存在 if(foodName.equals(food.getFoodName())) { //存在该菜品名,返回该菜品对象。结束后续代码运行 return food; } } //程序执行至此,说明没有找到对应的food对象,因此返回null return null; } //根据菜品id查询菜品信息 public Food getFoodById(String id) { //遍历foodList,以获取每个元素的foodId for(Food food : foodList) { //判断要查找的菜品ID是否存在 if(id.equals(food.getFoodId())) { //存在该菜品ID,返回该菜品对象。结束后续代码运行 return food; } } //程序执行至此,说明没有找到对应的food对象,因此返回null return null; } //根据菜品ID修改菜品 public void updateFood(Food newFood) { //取得要修改菜品的ID String foodId = newFood.getFoodId(); //遍历foodList for(Food food : foodList) { //根据获取到的Id,找到对应的元素 if(foodId.equals(food.getFoodId())) { //将newFood中要修改的信息赋值给原来的菜品 //newFood中除foodId属性外其它不为null的属性,即为要更新的,否则不予以更新 //foodName更新判断 if(newFood.getFoodName() != null && newFood.getFoodName() != "") { food.setFoodName(newFood.getFoodName()); System.out.println("菜名已更新"); } //price更新判断 if(newFood.getPrice() != 0.0) { food.setPrice(newFood.getPrice()); } //taste更新 if(newFood.getTaste() != null && newFood.getTaste() != "") { food.setTaste(newFood.getTaste()); System.out.println("口味已更新"); } //description更新 if(newFood.getDescription() != null && newFood.getDescription() != "") { food.setDescription(newFood.getDescription()); } //path更新 if(newFood.getPath() != null && newFood.getPath() != "") { food.setPath(newFood.getPath()); } } } } // 根据菜品ID进行删除 public void deleteFoodById(String id) { for (Food food : foodList) { //根据id找到对应的元素 if(id.equals(food.getFoodId())) { //移除该该元素 foodList.remove(food); //移除成功,结束遍历 break; } } } // public static void main(String[] args) { // Food food = new Food("f01","宫保鸡丁",36.5f,"旧口味","旧描述",null); // Food newfood = new Food("f01","辣子鸡",0.0f,"","好吃不贵",null); // FoodDaoImpl impl = new FoodDaoImpl(); // impl.addFood(food); // impl.updateFood(newfood); // } }
FoodAddServlet.java
package com.imooc.demo.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.demo.java.Food; import com.imooc.demo.utils.FileUploadUtil; /** * 添加菜品Servlet */ @WebServlet("/add") public class FoodAddServlet extends HttpServlet { /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { //定义一个字符串,用以存放文件上传后的完整路径 String url = null; //定义一个map容器,存放一次请求表单项的名称与值 Map<String,String> fileItemMap = new HashMap<String,String>(); //创建磁盘文件项工厂 DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); //创建核心解析类 ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory); //解析request请求,返回fileItem的集合 List<FileItem> fileItemList = servletFileUpload.parseRequest(request); //遍历fileItemList集合,判断其中的元素是普通表单项还是文件上传项 for (FileItem fileItem : fileItemList) { //判断fileItem是否为普通表单项 if(fileItem.isFormField()) { //普通表单项 //取得表单项的name属性值 String fileItemName = fileItem.getName(); //取得表单项中的值 String fileItemValue = fileItem.getString(); //将表单项加入到FileItemMap中 fileItemMap.put(fileItemName, fileItemValue); }else { //文件上传项 //取得文件上传名 String fileName = fileItem.getName(); //如果文件名不为空(即未上传文件)且不为空字符串,则进行上传工作 if(fileName != null && fileName.equals("")) { //获得唯一文件名 String uuidFileName = FileUploadUtil.getUUIDFileName(fileName); //取得文件数据 InputStream is = fileItem.getInputStream(); //获取文件上传路径 String path = this.getServletContext().getRealPath("/upload"); //将输入流与输出流对接 url = path + "\\" + 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(); } } } //从ServletContext域中,获取存放菜品对象的集合 List<Food> foodList =(List<Food>) this.getServletContext().getAttribute("list"); //遍历集合,检查新添加的菜品是否存在 for(Food food : foodList) { if(food.getFoodId().equals(fileItemMap.get("id"))) { request.setAttribute("msg", "菜品ID已经存在!"); request.getRequestDispatcher("/addFood.jsp").forward(request, response); } if(food.getFoodName().equals(fileItemMap.get("foodName"))) { request.setAttribute("msg", "菜品名已经存在!"); request.getRequestDispatcher("/addFood.jsp").forward(request, response); } } //封装数据到Food对象中去 Food food = new Food(fileItemMap.get("id"), fileItemMap.get("foodName"), Integer.parseInt(fileItemMap.get("price")), fileItemMap.get("taste"), fileItemMap.get("description"), fileItemMap.get("path")); foodList.add(food); this.getServletContext().setAttribute("list", foodList); request.getSession().setAttribute("food", food); response.sendRedirect(request.getContextPath()+"/showFoodList.jsp"); } catch (FileUploadException e) { e.printStackTrace(); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
InitServlet.java
package com.imooc.demo.servlet; import java.util.ArrayList; import java.util.List; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import com.imooc.demo.java.Food; import com.imooc.demo.java.FoodDaoImpl; /** * 菜品添加初始化Servlet */ @WebServlet(urlPatterns="/init",loadOnStartup=2) public class InitServlet extends HttpServlet { /** * 菜品添加初始化方法 */ public void init(ServletConfig config) throws ServletException { this.getServletContext().setAttribute("list", FoodDaoImpl.getFoodlist()); } }
0
收起
正在回答
3回答
同学你好,报空指针的是foodList集合吗?建议同学输出一下foodList,查看是否为空。另外这里需要定义初始化菜品的init()方法,直接将菜品添加到db中,对db进行操作即可。
祝:学习愉快~
懒人13
2019-07-31 16:46:08
尝试将FoodDaoImpl.java改为Servlet也报相同的错误。代码如下:
package com.imooc.demo.java; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; /** * 菜品数据处理类 * @author m1318 * */ @WebServlet(urlPatterns="/init",loadOnStartup=2) public class FoodDaoImpl extends HttpServlet{ //存放菜品对象的全局静态容器List private static final List<Food> foodList = new ArrayList<Food>(); // //foodList的get方法 // public static List<Food> getFoodlist() { // return foodList; // } // 菜品添加初始化方法 public void init(ServletConfig config) throws ServletException { this.getServletContext().setAttribute("list", foodList); } //添加菜品 public void addFood(Food food) { //判断要添加的food对象是否为空 if(food != null) { //food对象不为空,添加到foodList中 foodList.add(food); } } //查询所有菜品信息 public List<Food> getAllFood(){ //返回全局容器foodList List<Food> fl = new ArrayList<Food>(); for(Food food : foodList) { fl.add(food); } return fl; } //根据菜品名称查询菜品信息 public Food getFoodByName(String foodName) { //遍历foodList,以获取每个元素的foodName for(Food food : foodList) { //判断要查找的菜品名是否存在 if(foodName.equals(food.getFoodName())) { //存在该菜品名,返回该菜品对象。结束后续代码运行 return food; } } //程序执行至此,说明没有找到对应的food对象,因此返回null return null; } //根据菜品id查询菜品信息 public Food getFoodById(String id) { //遍历foodList,以获取每个元素的foodId for(Food food : foodList) { //判断要查找的菜品ID是否存在 if(id.equals(food.getFoodId())) { //存在该菜品ID,返回该菜品对象。结束后续代码运行 return food; } } //程序执行至此,说明没有找到对应的food对象,因此返回null return null; } //根据菜品ID修改菜品 public void updateFood(Food newFood) { //取得要修改菜品的ID String foodId = newFood.getFoodId(); //遍历foodList for(Food food : foodList) { //根据获取到的Id,找到对应的元素 if(foodId.equals(food.getFoodId())) { //将newFood中要修改的信息赋值给原来的菜品 //newFood中除foodId属性外其它不为null的属性,即为要更新的,否则不予以更新 //foodName更新判断 if(newFood.getFoodName() != null && newFood.getFoodName() != "") { food.setFoodName(newFood.getFoodName()); System.out.println("菜名已更新"); } //price更新判断 if(newFood.getPrice() != 0.0) { food.setPrice(newFood.getPrice()); } //taste更新 if(newFood.getTaste() != null && newFood.getTaste() != "") { food.setTaste(newFood.getTaste()); System.out.println("口味已更新"); } //description更新 if(newFood.getDescription() != null && newFood.getDescription() != "") { food.setDescription(newFood.getDescription()); } //path更新 if(newFood.getPath() != null && newFood.getPath() != "") { food.setPath(newFood.getPath()); } } } } // 根据菜品ID进行删除 public void deleteFoodById(String id) { for (Food food : foodList) { //根据id找到对应的元素 if(id.equals(food.getFoodId())) { //移除该该元素 foodList.remove(food); //移除成功,结束遍历 break; } } } // public static void main(String[] args) { // Food food = new Food("f01","宫保鸡丁",36.5f,"旧口味","旧描述",null); // Food newfood = new Food("f01","辣子鸡",0.0f,"","好吃不贵",null); // FoodDaoImpl impl = new FoodDaoImpl(); // impl.addFood(food); // impl.updateFood(newfood); // } }
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星