0

I'm a beginner in web-development. I want to send the data to the servlet, but I got an NullPointerException .

For example the value of nickname = admin and I can see it. When I press the button to post to the servlet I can't pass data it seams that all data are null. I can't understand why. I have load the data from another (jsp + servlet) to this that I'm posting.

Jsp

         <form name="admin_view_user" action="${pageContext.request.contextPath}/admin/users/user/activate" method="POST">
        <div id="leftcolumn">
        <div>
            <img src="${pageContext.request.contextPath}/images/imageholder_detailspage.jpg" alt="" width="220" height="220" class="previewimg" />
        </div>

        <c:choose>
            <c:when test="${active =='1'}">
                <h2>Ok</h2>
                <br />
            </c:when>    
            <c:otherwise>
                <h2>Post</h2>

                <div>
                    <input class="commitbutton" type="submit" alt="buttonActivete" name="buttonActivete" id="buttonActivete" value="Activet" />
                </div>

                <br />
            </c:otherwise>
        </c:choose>
    </div>

    <div id="rightcolumn">
        <table class="table">
            <tr>

                <td width="10%" class="td_attributes">Nickname</td>
                <td name="nickname" id="nickname" class="text longfield" value="${nickname}">
                    <c:out value="${nickname}"/>
                </td>

            </tr>

        </table>
    </div>
    <div class="clear">&nbsp;</div>
</form>
</div>

Servlet

@WebServlet(name = "AdminActivate", urlPatterns = {"/admin/users/user/activate"})
 public class AdminActivate extends HttpServlet {

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    ServletContext servletContext = getServletContext();
    try (PrintWriter out = response.getWriter()) {
        ViewUserAdminActivet controller = new ViewUserAdminActivet();
        Map<String, String[]> parameterMap = request.getParameterMap();
        String nickname = parameterMap.get("nickname")[0];
        System.out.println(nickname + "________________________________");
       // controller.execute(servletContext, parameterMap, request, response);
    }
    }

In this line I got the exception Map<String, String[]> parameterMap = request.getParameterMap();

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
psilos
  • 101
  • 1
  • 11
  • Obviously you don't have the `nickname` parameter in the map. So when you try to get 0 element from null array you get the NPE. Check whether it is really sent. – StanislavL Aug 24 '17 at 14:59
  • 1
    Where is input field with "nickname" name in your jsp? Because of this you can't get anything from `parameterMap` with attribute name like "nickname". – Aleksandr Podkutin Aug 24 '17 at 15:02
  • input type="text" name="nickname" id="nickname" class="text longfield" value="${nickname}" is this the correct solutin to my problem.Can i have an example or link, to understend better. – psilos Aug 24 '17 at 16:29

0 Answers0