为什么会报空指针呢,大神速进来

帅狗 发布于 2013/12/13 12:18
阅读 2K+
收藏 0

在187行 OutputStream os = response.getOutputStream() 这句话

java.lang.NullPointerException报这个异常

package com.twinflag.mms.web.action;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.twinflag.mms.base.BaseAction;
import com.twinflag.mms.entity.TOperationlog;
import com.twinflag.mms.service.LogService;
import com.twinflag.mms.util.ExcelFileGenerator;

@SuppressWarnings("serial")
@Controller("LogAction")
@Scope(value = "prototype")
public class LogAction extends ActionSupport implements SessionAware,  
ServletRequestAware, ServletResponseAware{//BaseAction<TOperationlog>

	@Resource(name = LogService.SERVICE_NAME)
	private LogService logService;
	private int totalProperty;
	private int start;
	private int limit;

	private String TId;
	private String[] deleIds;

    public  HttpServletResponse response;//这里我只需要response所以就注入了
	
	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public String getTId() {
		return TId;
	}

	public void setTId(String tId) {
		TId = tId;
	}

	public String[] getDeleIds() {
		return deleIds;
	}

	public void setDeleIds(String[] deleIds) {
		this.deleIds = deleIds;
	}

	private List<TOperationlog> pageList = new ArrayList<TOperationlog>();

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public List<TOperationlog> getPageList() {
		return pageList;
	}

	public void setPageList(List<TOperationlog> pageList) {
		this.pageList = pageList;
	}

	public int getTotalProperty() {
		return totalProperty;
	}

	private List<TOperationlog> tempList;
	private String str;

	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}

	public List getRoot() {
		return tempList;
	}

	public List getTempList() {
		return tempList;
	}

	public void setTempList(List<TOperationlog> tempList) {
		this.tempList = tempList;
	}

	HttpServletRequest request = ServletActionContext.getRequest();

	public void setTotalProperty(int totalProperty) {
		this.totalProperty = totalProperty;
	}

	public String log() {
		System.out.println("进来log");
		if (StringUtils.isBlank(str)) {
			this.tempList = logService.log();
			this.totalProperty = tempList.size();
			int end = start + limit;
			if (end > totalProperty) {
				end = totalProperty;
			}
			for (int i = start; i < end; i++) {
				// tempList.get(i).setTUser(null);
				pageList.add(tempList.get(i));
			}
		} else {
//
//			List<TUser> user = m.findUserByName(str);
//			Integer id = user.get(0).getTId();

			this.tempList = logService.findLogByName(str);
			this.totalProperty = tempList.size();
			int end = start + limit;
			if (end > totalProperty) {
				end = totalProperty;
			}
			for (int i = start; i < end; i++) {
				// tempList.get(i).setTUser(null);
				pageList.add(tempList.get(i));
			}

		}
		return SUCCESS;
	}

	public String delete() {
		// System.out.println("进来delete了  Tid = "+TId);
		logService.deleteLogById(TId);
		return SUCCESS;
	}

	public String deleteMore() {
		System.out.println("进来deletemore");
		logService.deleteMoreLog(deleIds);
		return SUCCESS;
	}

	public String export(){
		System.out.println("进来export");
		String search = request.getParameter("search");
		ArrayList<String> fieldName = logService.exportExcelTitle();
		ArrayList<ArrayList<String>> fieldData = logService.exportExcelData(search);
		ExcelFileGenerator excelFileGenerator = new ExcelFileGenerator(fieldName,fieldData);
		try {
			OutputStream os = response.getOutputStream();//为什么这里会报空指针呢
			System.out.println("resoponse");
			response.reset();
			response.setContentType("application/vnd.ms-excel");
			String filename = URLEncoder.encode("操作日志", "utf-8");
			response.setHeader("Content-disposition", "attachment; fileName="+filename);
			excelFileGenerator.expordExcel(os);
			os.flush();
			if (os!=null) {
				os.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		
	}
}
加载中
0
修改登录密码
修改登录密码
java也有指针阿
修改登录密码
修改登录密码
回复 @瓣挚锅 : 你的标题里说有空指针 那就是说java有指针呗
帅狗
帅狗
???什么啊,没有吧,答非所问
0
Jerikc
Jerikc

public void setResponse(HttpServletResponse response) {...}

这里面打log,看看传进来的response是否是null

0
张亦俊
张亦俊

这是调用code的问题,只有这个code看不出来的。

如果调用的code没有在使用export之前调用setResponse,这个response一定是空的,你自己看看调用的code吧

0
淘淘我的小宝宝
淘淘我的小宝宝
指的是空引用,类似指针吧。翻译问题
0
淘淘我的小宝宝
淘淘我的小宝宝
你的response自己生成了get set方法,set方法应该在override 的方法里设置action定义的response.我看你现在override方法里啥都没干,不空才怪。
0
YuKunYi
YuKunYi
把你49行的代码放到221行,然后把219行的arg0改成response应该就好了吧。。。注入用的是 ServletResponseAware接口。。。
0
常氏孤儿
常氏孤儿
你response已声明提供了set get ,还怎么空啊,空你妹啊!
帅狗
帅狗
何弃疗???
返回顶部
顶部