0

I have a form to which radio inputs are generated using js code. But when I try to submit the form, the selected value is not being sent to servlet and I am getting null pointer exception.

onMyPage.jsp

<form action="generate" method="post">
<%
for (Monitor mtr : maInfo.getMonitor()) {
    out.println("<input style=\"margin-left:20px\" type=\"radio\" name=\"monitor\" value=\""+mtr.getMonitorAbbr()+"\"/>"+mtr.getMonitorAbbr()+"<br>");
}
%>
<input type="submit" value="Submit" onclick="this.disabled=true"/>
</form>

On servlet.java

String variable = (String) request.getAttribute("monitor");

Error message:

java.lang.NullPointerException: Cannot invoke "Object.toString()" because the return value of "javax.servlet.http.HttpServletRequest.getAttribute(String)" is null
2
  • Instead of request.getAttribute("monitor") use request.getParameter("monitor"). Commented Jan 21, 2021 at 5:42
  • Thank you @Swati. I was able to fix with your suggestion. Commented Jan 21, 2021 at 12:49

1 Answer 1

0

Request.getAttribute() is basically used to get Request Header or field values in request. Attribute can be also be used to set some objects in Request scope. This helps in request forwards.

Request.getParameter() is used to retrieve form parameters of HTTP Request.

pls refer HTTPServletRequest documentation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.