2012-03-15 56 views
0

在我的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是我使用從客戶端到服務器傳遞用戶名的參數。

+0

您可以粘貼異常的堆棧跟蹤嗎?另外是logUserName你使用從客戶端到服務器傳遞用戶名的參數?實際操作中拋出異常的代碼也會有幫助! – 2012-03-15 07:12:14

+0

這是絕對不可讀的。你能編輯原始問題並在那裏添加代碼 – 2012-03-15 16:18:39

+0

@ vishal.biyani抱歉。我會編輯原始文章 – Bishan 2012-03-16 01:10:02

回答

1

請嘗試下面的代碼。

HttpSession hs = req.getSession(false); 
if(hs == null)System.out.println("session is null"); 
else { 
    String name = hs.getAttribute("logUserName"); 
    if(name == null)System.out.println("name is null"); 
    else System.out.println(name);