HttpSession session = 报空指针异常(request=null)

时间:2025-03-27 19:40:50

刚才手残误把浏览器关了,本来好好的一篇博客没保存就没了!

1.问题

HttpSession session = ();这一行报空指针异常,

打断点调试,发现request=null。

2.原因:

2.1 OeQuestionAction extends ActionSupport implements ServletRequestAware{      },在类中定义private修饰的变量的时候没有生成相应的getter()和setter()方法;

2.2在OeQuestionAction 类重写的方法中没有设置正确的参数,并且没有将当前的request对象绑定到本类需要的request对象上,导致request对象为空,HttpSession session = ();无法获得当前session。

3.解决问题后:

package ;

import ;

import ;
import ;

import .;
import .;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

import .;
public class OeQuestionAction extends ActionSupport implements ServletRequestAware {
	
	private static final long serialVersionUID = 1L;
	
	private HttpServletRequest request;
	
	private OeQuestionDao oequestionDao = new OeQuestionDao();
	private OePaperDao oepaperDao = new OePaperDao();
	
	private List<OeQuestion> oequestionList;
	private List<OePaper> oepaperList;
	private String mainPage;
	
	private String oequestionId;
	private String title;
	private OeQuestion oequestion;
	
	private String page;
	private String pageCode;
	//全部试题数
	private int total;
	
	private OeQuestion oq_e;
	
	
	
	public List<OeQuestion> getOequestionList() {
		return oequestionList;
	}

	public void setOequestionList(List<OeQuestion> oequestionList) {
		 = oequestionList;
	}

	public List<OePaper> getOepaperList() {
		return oepaperList;
	}

	public void setOepaperList(List<OePaper> oepaperList) {
		 = oepaperList;
	}

	public String getMainPage() {
		return mainPage;
	}

	public void setMainPage(String mainPage) {
		 = mainPage;
	}

	public String getOequestionId() {
		return oequestionId;
	}

	public void setOequestionId(String oequestionId) {
		 = oequestionId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		 = title;
	}

	public OeQuestion getOequestion() {
		return oequestion;
	}

	public void setOequestion(OeQuestion oequestion) {
		 = oequestion;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		 = page;
	}

	public String getPageCode() {
		return pageCode;
	}

	public void setPageCode(String pageCode) {
		 = pageCode;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		 = total;
	}

	public OeQuestion getOq_e() {
		return oq_e;
	}

	public void setOq_e(OeQuestion oq_e) {
		this.oq_e = oq_e;
	}

	public String show()throws Exception{
		
		//session是什么?session就是一次请求的载体,并且在创建这个session的时候会给她创建一个标识号(就像人的身份证一样)!
		HttpSession session = ();
		if((page)) {
			page = "1";
		}
		if(oq_e != null) {
			("oq_e", oq_e);
		}else {
			Object o = ("oq_e");
			if(o != null) {
				oq_e = (OeQuestion) o;
			}else {
				oq_e = new OeQuestion();
			}
		}
		//OePage oepage = new OePage((page),dataNum);
		OePage oepage = new OePage((page),(("pageSize")));
		oequestionList = (oq_e,oepage);
		total = (oq_e);
		pageCode = (()+"/oequestion!show", total, (page), (("pageSize")));
		mainPage = "oequestion/";
		return SUCCESS;
	}

	public String getOeQuestionById() {
		oequestion = (oequestionId);
		mainPage = "oequestion/";
		return SUCCESS;
	}
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		 = request;
	}
}