报错信息显示36行错误,请老师帮忙看看是什么错误

报错信息显示36行错误,请老师帮忙看看是什么错误

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8" import="java.util.*,com.imooc.bean.*"%>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>菜品信息展示</title>
<style type="text/css">
</style>
</head>
<body>
	<%
		String foodname = "";
		if (session.getAttribute("foodname") != null) {
			foodname=(String) session.getAttribute("foodname");
		}
		
		
	%>
	<center>
		<h1>菜品查询</h1>
		<table border="1px" cellspacing="0px" cellpadding="0px" width="800px">
			<thead>
				<tr>
					<th>菜品ID</th>
					<th>菜名</th>
					<th>口味</th>
					<th>菜品图片</th>
					<th>价格</th>
					<th>菜品描述</th>
				</tr>
			</thead>
			<tbody>
			<%List<Food> foodlist=(List<Food>)request.getAttribute("foodlist");
			for(Food food:foodlist){
				Object id=food.getFoodid();
				Object name=food.getFoodname();
				Object taste=food.getFoodtaste();
				String filepath=food.getPath();
				Object price=food.getPrice();
				Object describe=food.getDescribe();
			
			%>
				<tr>
					<td><%=id %></td>
					<td><%=name %></td>
					<td><%=taste %></td>
					<td><%=filepath %></td>
					<td><%=price %></td>
					<td><%=describe %></td>
					
				</tr>
				<%} %>
			</tbody>
		</table>
	</center>
</body>
</html>
package com.imooc.servlet;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
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.bean.Food;
import com.imooc.utils.UploadUtils;

@WebServlet("/ManageServlet")
public class ManageServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			//数据的接收,文件上传基本操作
			//创建一个磁盘文件项工厂对象
			DiskFileItemFactory diskfileitemfactory=new DiskFileItemFactory();
			//创建一个核心解析类
			ServletFileUpload servletfileupload=new ServletFileUpload(diskfileitemfactory);
			//解析request请求,返回List集合,list集合中存放FileItem对象
			List<FileItem> list=servletfileupload.parseRequest(request);
			//封装数据完成基本的上传功能,定义一个Map集合,用于保存接收到的数据
			Map<String,String> map=new HashMap<String,String>();
			String url=null;
			//遍历集合,获得每个FileItem,判断是表单项还是文件上传项
			for(FileItem fileItem:list) {
				if(fileItem.isFormField()) {
					//获得表单项name值
					String name=fileItem.getFieldName();
					//获得表单项value值
					String value=fileItem.getString("UTF-8");
					System.out.println(name+" "+value);
					//接收复选框参数
					//定义一个List集合保存口味
					List<String> foodtastelist=new ArrayList<String>();
					if("foodtaste".equals(name)) {
						String foodtastevalue=fileItem.getString("UTF-8");
						//接收到一个值,将这个值存入到foodtastelist中
						foodtastelist.add(foodtastevalue);
						foodtastevalue=foodtastelist.toString().substring(1,foodtastelist.toString().length()-1);
						System.out.println(name+" "+foodtastevalue);
						//将口味的数据存入到Map集合中
						map.put(name, foodtastevalue);
					}else {
						map.put(name, value);
					}
				}else {
					//文件上传项,文件上传功能,获得文件上传名称
					String fileName=fileItem.getName();
					if(fileName!=null&&!"".equals(fileName)) {
						//通过工具类获取唯一文件名
						String uuidFilename=UploadUtils.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();
					}
					
				}
			}
			System.out.println(map);
			//将菜品信息存入到List集合中,首先获得ServletConText对象
			List<Food> foodlist=(List<Food>) this.getServletContext().getAttribute("list");
			//检查菜品是否存在
			for(Food f:foodlist) {
				if(f.getFoodname().equals(map.get("foodname"))) {
					request.setAttribute("msg", "该菜品已存在!");
					//跳转
					request.getRequestDispatcher("/addFood.jsp").forward(request, response);
				}
			}
			//封装数据到Food当中
			Food food=new Food();
			food.setFoodid(map.get("foodid"));
			food.setFoodname(map.get("foodname"));
			food.setFoodtaste(map.get("foodtaste"));
			food.setPrice(map.get("price"));
			food.setDescribe(map.get("describe"));
			food.setPath(url);
			
			foodlist.add(food);
			for(Food f:foodlist) {
				System.out.println(f);
			}
			this.getServletContext().setAttribute("list", foodlist);
			//菜品添加成功,跳转到显示界面
			request.getSession().setAttribute("foodname", food.getFoodname());
			response.sendRedirect(request.getContextPath()+"/showFoodList.jsp");
			
            
			
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		doGet(request, response);
	}

}
package com.imooc.bean;

/*
 * 菜品类
 */
public class Food {
	private String foodid;
	private String foodname;
	private String foodtaste;
	private String price;
	private String describe;
	private String path;

	public Food() {

	}

	public Food(String foodid, String foodname, String foodtaste, String price, String describe, String path) {
		super();
		this.foodid = foodid;
		this.foodname = foodname;
		this.foodtaste = foodtaste;
		this.price = price;
		this.describe = describe;
		this.path = path;
	}

	public String getFoodid() {
		return foodid;
	}

	public void setFoodid(String foodid) {
		this.foodid = foodid;
	}

	public String getFoodname() {
		return foodname;
	}

	public void setFoodname(String foodname) {
		this.foodname = foodname;
	}

	public String getFoodtaste() {
		return foodtaste;
	}

	public void setFoodtaste(String foodtaste) {
		this.foodtaste = foodtaste;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public String getDescribe() {
		return describe;
	}

	public void setDescribe(String describe) {
		this.describe = describe;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	@Override
	public String toString() {
		return "Food [foodid=" + foodid + ", foodname=" + foodname + ", foodtaste=" + foodtaste + ", price=" + price
				+ ", describe=" + describe + ", path=" + path + "]";
	}

}

http://img1.sycdn.imooc.com//climg/5bc3f5920001d01919191027.jpg

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
提问者 精慕门6573819 2018-10-15 12:20:00
  • 建议同学把 this.getServletContext().setAttribute("list", foodlist); 改为 request.setAttribute("list", foodlist); 试试。 祝学习愉快
    2018-10-15 14:50:25
好帮手慕阿莹 2018-10-15 11:37:12

从从报错来看,是空指针异常。如下图所示,同学存的时候,是list ,取得时候,应该用list取,而不是foodlist。

<%List<Food> foodlist=(List<Food>)request.getAttribute("foodlist"); 改为

<%List<Food> foodlist=(List<Food>)request.getAttribute("list"); 试试


http://img1.sycdn.imooc.com//climg/5bc40b1a0001f2cd06680290.jpg

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
从网页搭建入门Java Web2018版
  • 参与学习           人
  • 提交作业       1088    份
  • 解答问题       10204    个

如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师