Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic