2016-03-28 219 views
-1

我將在JSP頁面中獲得的變量值傳遞給servlet頁面。我使用會話。但它給出以下NullPointerException將變量值從JSP傳遞到Servlet

java.lang.NullPointerException 
    com.google.api.client.http.GenericUrl.appendParam(GenericUrl.java:599) 
    com.google.api.client.http.GenericUrl.addQueryParams(GenericUrl.java:582) 
    com.google.api.client.http.UriTemplate.expand(UriTemplate.java:346) 
    com.google.api.client.http.UriTemplate.expand(UriTemplate.java:259) 
    com.google.api.client.googleapis.services.AbstractGoogleClientRequest.buildHttpRequestUrl(AbstractGoogleClientRequest.java:266) 
    com.google.api.client.googleapis.services.AbstractGoogleClientRequest.buildHttpRequest(AbstractGoogleClientRequest.java:301) 
    com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419) 
    com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352) 
    com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469) 
    com.translator.api.TranslatorApi.getTranslate(TranslatorApi.java:66) 
    control.TextTranslation.doPost(TextTranslation.java:47) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:650) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731) 
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 

imageExtract.jsp

</div> 
     <div class="row"> 
      <div id = "display" class="col-lg-8 center-block "> 
      <% 
      String extracted = (String) request.getAttribute("extractedText"); 
      session.setAttribute("text", extracted); 

      %> 
       <textarea maxlength="200" class=" form-control " name="msg" rows="20" id="Extract-textarea" ><%=extracted %> </textarea> 

      </div> 
    </div> 

TextTranslate.java

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

    PrintWriter out = response.getWriter(); 
    String language = "ES"; 
    HttpSession extractedText = request.getSession(); 
    String imageName = (String) extractedText.getAttribute("extractedText"); 
    System.out.println("this is so stupid"); 
    System.out.println("This is "+imageName); 
    TranslatorApi translation = new TranslatorApi(); 

    String outputText = translation.getTranslate(imageName, language); 
    System.out.println("Second"+outputText); 
    request.setAttribute("outputText", outputText); 
    RequestDispatcher sndValue = request.getRequestDispatcher("/translation.jsp"); 
    sndValue.forward(request, response); 

} 

的問題是,會話值不會傳遞到Servlet,這給NullpointerException。爲什麼會發生這種情況?

+0

哪個值的問題? 'text'或'extractedText'? – brso05

+0

請避免在JSP中使用普通的java。 JSTL在它內部替換Java方面做得很好。 –

+0

您正在設置一個名爲'text'的會話屬性,並*獲得一個名爲'extractedText'的會話屬性。這就是爲什麼它是'空'。 –

回答

2

正在設置的會話變量的名稱只是「文本」,但您試圖訪問代碼中的「extractedText」屬性。 「文本」屬性根本不被訪問,只是設置。

上面的代碼中似乎沒有設置會話中的extractedText屬性。因此,它只是簡單地通過imageName變量向您的Google API返回空值,然後這當然會失敗。

0

您只能獲取已設置的屬性值,您已設置outputText的屬性,因此您需要獲取該屬性值。

request.setAttribute("outputText", outputText); 

更改您的代碼來糾正的getAttribute

<div class="row"> 
     <div id = "display" class="col-lg-8 center-block "> 
     <% 
     String extracted = (String) request.getAttribute("outputText"); 
     session.setAttribute("text", extracted); 

     %> 
      <textarea maxlength="200" class=" form-control " name="msg" rows="20" id="Extract-textarea" ><%=extracted %> </textarea> 

     </div> 
</div> 

使用JSTL是標準,並幫助清理你的HTML代碼..

使用JSTL你的代碼,所以你可以看到有多少是簡單正在使用jstl。

<div class="row"> 
     <div id = "display" class="col-lg-8 center-block "> 
      <textarea maxlength="200" class=" form-control " name="msg" rows="20" id="Extract-textarea" > 
      <c:out value="${outputText}"> 
      </textarea> 
     </div> 
    </div> 

Simple tutorial for using and setting up jstl