-1

I am passing a variable value which is gained with in JSP page to a servlet page. I used sessions. But it gives the following NullPointerException.

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);

}

The problem is that the session value is not passed to the servlet, which gives a NullpointerException. Why might this be happening?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103

2 Answers2

2

The name of the session variable that is being set is just "text", but you are trying to access the "extractedText" attribute in your code. The "text" attribute isn't being accessed at all, merely set.

There appears to be nothing in your code above that sets the extractedText attribute in your session. Hence, it's simply returning null to your Google API, via the imageName variable, which then of course fails.

ManoDestra
  • 6,325
  • 6
  • 26
  • 50
0

You can only get the Attribute values you have set, you have set an attribute of outputText so you need to get that attribute value.

request.setAttribute("outputText", outputText);

Change your code to correct 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>

Using JSTL is the standard and helps clean up your html code..

your code using jstl so you can see how much simpler it is using 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

Kyle Bradley
  • 141
  • 1
  • 10