0

I have a JSP page which is a table full of forms. When I click submit it goes to a servlet which is suppose to read the values of the forms in the survey using the getParamValues method but it is not working. When I try to print out the String array from the getParamValues I keep getting nullpointerexceptions.

Here is the relevant servlet code:

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    resp.setContentType("text/plain");
    PrintWriter out = resp.getWriter();
    String[] nums = req.getParameterValues("questNum");
    out.println(nums[2]);
}

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    doPost(req, resp);
}

Here is the relevant JSP code, running on GAE:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
 <%@ page import="com.surveycreator.service.persistence.SurveyPersistence" %>
 <%@ page import="com.surveycreator.service.model.Survey" %>
 <%@ page import="com.surveycreator.service.model.SurveyQuestion" %>
 <%@ page import="com.surveycreator.service.model.Survey" %>
 <%@ page import="java.util.List" %>


<html>
<head>
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
</head>

<body>
<p>View Survey</p>
<form action="/storesurvey" method="post">
<input type="submit" value="Submit Survey" />
</form>




<%
SurveyPersistence persist = new SurveyPersistence();
String surveyName = request.getParameter("content");
Survey survey = persist.getSurvey(surveyName);
List<SurveyQuestion> question = survey.getSurvey();
out.println("<table border='1'><tr><th>QuestionNumber</th><th>Survey Question</th><th>(Yes/yes)(No/no)</th></tr>");
for (SurveyQuestion questionObject : question) {
    int tableCounterString = questionObject.getQuestionNumber();
    String quest = questionObject.getQuestion();
    out.println("<tr><td><form><input id ='question' name='questNum' type='text' readonly='readonly' value='"+tableCounterString + " ' /></form></td><td><form><input id ='question' name='quest' type='text' readonly='readonly' value='"+quest + " ' /></form></td><td><form><input id ='ans' name='answer' type='text' value='' /></form></td></tr>");


}  


%>
<%= survey.getSurveyName()%>

</body>
</html>
1
  • That link leads to a HTTP 500 error page and is not particularly helpful anyway. Just post the relevant source code.
    – BalusC
    Mar 28, 2011 at 15:46

2 Answers 2

0

Based on the information given as far, the only possible cause of a NPE is that you simply don't have any <input>, <select> or <textarea> element with a name="questNum" anywhere in the HTML form which submits to the servlet.


Update as per the JSP code: the submit button is indeed in a different HTML form than the input fields are. You need to put the submit button in the same HTML form as the input field is. Depending on the functional requirement -which is unclear at this point-, you need to move the submit button into the table cell, or to remove the <form> in the table cell and wrap the entire table inside a big <form>.


Unrelated to the concrete problem, mixing doGet() and doPost() is a poor practice in this particular case. If you ever open the servlet by entering its URL in browser address bar, without passing any parameters, then you will definitely get NPEs. Don't let them do the same. Use doGet() only to preprocess requests and use doPost() only to postprocess requests. As an example, all the Java code which you have inside the JSP scriptlet <% %> should be done in the doGet() method and the HTML code should be repeated by JSTL <c:forEach> tag.

1
  • I am sorry the link does not work, here is the code for the JSP, i should mention everything is on the Google App Engine.
    – Delanoy
    Mar 28, 2011 at 16:08
0

I think there are many forms in your code. when you press Submit Survey button, there is no value will be sent to the servlet but the value of button.

Solution: (base on your provided code )

     <head>
     <link type="text/css" rel="stylesheet"
 href="/stylesheets/main.css" />
     </head>

     <body>
     <p>View Survey</p>
     <form action="/storesurvey" method="post">
     <input type="submit" value="Submit Survey" />

     <%
     SurveyPersistence persist = new SurveyPersistence();
     String surveyName = request.getParameter("content");
     Survey survey = persist.getSurvey(surveyName);
     List<SurveyQuestion> question = survey.getSurvey();
     out.println("<table border='1'><tr><th>QuestionNumber</th><th>Survey Question</th><th>(Yes/yes)(No/no)</th></tr>");
     for (SurveyQuestion questionObject : question) {
         int tableCounterString = questionObject.getQuestionNumber();
         String quest = questionObject.getQuestion();
         out.println("<tr><td><input id
 ='question' name='questNum' type='text' readonly='readonly'
 value='"+tableCounterString + " '
 /></td><td><input id
 ='question' name='quest' type='text' readonly='readonly' value='"+quest + "
 ' /></form></td><td><input id
 ='ans' name='answer' type='text' value='' /></td></tr>");


     }  


     %>
</form> <-----(this is the change)
     <%= survey.getSurveyName()%>

     </body>
     </html>

Notice that I removed some tag in your code.

try it out. If there is bug, please post here. Hope this help.

NathanPhan

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.