This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Radio problems?

 
Tom Barns
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I'm trying to have two radios on my jsp page,for example i have radio1 and radio2,i need when i check radio1 to pop up a jsp page
that displays radio1 is on,the same when i check radio2 in need to pop up a jsp page that displays radio2 is on.
I'm trying that and i'm getting internal servlet error(java.lang.nullpointerException),iknow that i'm working on reference without initializing it.
How can i make it work?
thanks for your time.
Here is my source code:
//====================
<html>
<form action="radioResponse.jsp">
<body bgcolor="#996600">
radio1 <input type="radio" name="radio1" ><br>
radio2 <input type="radio" name="radio2" ><br>
<input type="submit" >
</form>
</body>
</html>
//===========================
//==========================
<%if(request.getParameter("radio1").equals("on")){ %>
radio1 is on
<%}else{%>
radio1 is of
<%}%>
<%if(request.getParameter("radio2").equals("on")){ %>
radio2 is on
<%}else{%>
radio2 is off
<%}%>
//============================
 
Bhupinder Dhillon
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tom Barns:

<%if(request.getParameter("radio1").equals("on")){ %>
radio1 is on
<%}else{%>
radio1 is of
<%}%>
<%if(request.getParameter("radio2").equals("on")){ %>
radio2 is on
<%}else{%>
radio2 is off
<%}%>


If there is no parameter in the request then request.getParameter will always return null. So first time you access the page there is no parameter named radio1 or radio2 and you end up getting null references and calling .equals on null gives you nullpointer exception.
Here is the proper way...
<%
String radio1 = request.getParameter("radio1");
String radio2 = request.getParameter("radio2");
if(radio1 == null){ %>
radio1 is of
<%}else{%
radio1 is on
<%}%>
<%
if(radio2 == null){ %>
radio2 is off
<%}else{%>
radio2 is on
<%}%>
 
Tom Barns
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot,it worked.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic