未赋值的session在jsp报错,求大神帮帮忙吧

想要在页面打开是判定一个session会话,这个session要等到跳转到服务器之后才能赋值。
但是明明我设定了判断是否为空,可是还是报错。
这是我的页面和服务器代码:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<title>请填写上传表单</title>


<%
Object o = session.getAttribute("Error1");
if(!o.equals(null)||(!"".equals(o))){
String er = (String)o;
%>
    操作失败,<%=er %>
<%
}

%>


关键字:

简要描述(10个字,可以不写):

文章照片:






package com.hcpolic.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hcpolic.entity.Artical;
import com.hcpolic.entity.User;
import com.hcpolic.rbac.Opreator;
import com.hcpolic.rbac.Permission;
import com.hcpolic.util.Tools;

import org.apache.commons.fileupload.disk.*;
import org.apache.commons.fileupload.servlet.*;
import org.apache.commons.fileupload.*;

public class UploadServlet extends HttpServlet {

/**
 * The doGet method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to get.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    this.doPost(request, response);
}

/**
 * The doPost method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to post.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        HttpSession session=request.getSession();
        User us = (User)request.getSession().getAttribute("user");
        String rem = (String)request.getAttribute("remark");
        String key = (String)request.getAttribute("keyw");
        String er = null;
        Tools t = new Tools();
        ////////////////////////////////保存上传文件至指定文件夹
        String savePath = "D:/upload/";
        DiskFileItemFactory fac = new DiskFileItemFactory();
        fac.setRepository(new File(savePath+"uploadtemp"));
        ServletFileUpload upload = new ServletFileUpload(fac);
        upload.setFileSizeMax(1048576);
        try {
            List<FileItem> it = upload.parseRequest(request);
            Iterator<FileItem> iter = it.iterator();

            while(iter.hasNext()){
                FileItem item = iter.next();
                String fieldName = item.getFieldName();
                if(!item.isFormField()){
                    File saveFile = null;
                    InputStream input = null;
                    OutputStream output = null;
                    savePath = savePath + File.separator+String.valueOf(t.randomId(9,6))+"."+item.getName().split("\\.")[1];
                    input = item.getInputStream();
                    output = new FileOutputStream(new File(savePath));
                    byte data[] = new byte[512];
                    int temp = 0;
                    while((temp = input.read(data,0,512))!=-1){
                        output.write(data);
                    }
                    input.close();
                    output.close();
                }

            }
        } catch (FileUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ////////////////////////////////
        ///////////////////////////////抓取系统时间
        String strDate = null;
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        strDate = sf.format(new Date());
        ///////////////////////////////
        Artical a = new Artical();
        Permission per = new Permission();
        Opreator o = new Opreator();
        a.setId(us.getId().substring(4, 10)+String.valueOf(t.randomId(9,6)));
        a.setKeyWords(key.trim());
        a.setRemark(rem);
        a.setImagePath(savePath);
        a.setUpdateTime(strDate.trim());

        boolean ret = o.isAdd(a, us);
        if(ret){
            response.sendRedirect("main.jsp");
        }else{
            er = "您的权限不够!!";
            session.setAttribute("Error1", er);
            response.sendRedirect("Upload.jsp");
        }
}

}

1个回答

谔谔。小伙不理解空指针怎么来啊。空指针 是指调用 null值的对象或方法。懂了吗?
你那个o 的值 是 null 你调用 o的 equals 方法 等于 这么写 null.equals(); 当然会空指针。 判断是否为空就
o == null 或 o != null 这样写就行了。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问