File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Radio problems? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Radio problems?" Watch "Radio problems?" New topic
Author

Radio problems?

Tom Barns
Ranch Hand

Joined: Oct 27, 2000
Posts: 138
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
<%}%>
//============================


Thanks for your help.
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
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

Joined: Oct 27, 2000
Posts: 138
Thanks alot,it worked.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Radio problems?