• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Radio problems?

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
<%}%>
//============================
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot,it worked.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic