在我的Google App Engine
应用程序中,登录的用户可以上传他们的图片及其他详细信息。我用HttpSession
来管理日志记录。当登录的用户填写表格并选择要上传的照片并点击保存按钮时,由于Session
为空,将抛出NullPointerException
。 (数据存储在数据库中,但在数据存储到数据库后,尝试转发到成功页面时引发NullPointerException)。HttpSession空BlobstoreService
Form action = `blobstoreService.createUploadUrl("https://stackoverflow.com/users/adddetails")`
我已经把System.out.println(hs.getAttribute("logUserName") + "");
在servlet
这是点击保存按钮后触发的顶部。输出是null
。
我该如何避免这种情况?
修订
之后记录了用户填写表单,正如我在我的问题提到和数据添加到数据库中去(/users/adddetails
)路径。它工作正常。在添加数据后,使用resp.sendRedirect()
方法重定向到另一个servlet。在那个servlet上,我正在检查Session是否为null如下。
try
{
HttpSession hs = req.getSession();
if(!hs.getAttribute("logUserName").equals(null)) {
RequestDispatcher d = req .getRequestDispatcher("/WEB-INF/userhome/add_data.jsp");
d.forward(req, resp); }
else {
RequestDispatcher d = req .getRequestDispatcher("user_login.jsp");
d.forward(req, resp);
}
} catch (NullPointerException ex) {
// NullPointerException thrown from here
RequestDispatcher d = req.getRequestDispatcher("user_login.jsp");
d.forward(req, resp);
}catch (Exception ex) {
RequestDispatcher d = req.getRequestDispatcher("/msg/exception.jsp");
d.forward(req, resp);
}
NullPointerException
从在上面的代码中catch
块抛出。 logUserName
是我使用从客户端到服务器传递用户名的参数。
您可以粘贴异常的堆栈跟踪吗?另外是logUserName你使用从客户端到服务器传递用户名的参数?实际操作中抛出异常的代码也会有帮助! – 2012-03-15 07:12:14
这是绝对不可读的。你能编辑原始问题并在那里添加代码 – 2012-03-15 16:18:39
@ vishal.biyani抱歉。我会编辑原始文章 – Bishan 2012-03-16 01:10:02