在我的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