• 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

Checkbox err..

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I tried a lot but not getting ..Please help me.
There is checkBox.html which has $ checkboxes (name =Ans)
Here is CheckBox.java code
--------------------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.sql.*;
public class CheckBox extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
String ans="";
String[] selectedAns = request.getParameterValues("Ans");
if(selectedAns.length >= 1)
{
StringBuffer answers=new StringBuffer(1024);
for(int i=0;i<selectedAns.length;i++)
answers.append(selectedAns[i]);
out.println("selected answer : " +answers + "<P>");
ans=answers.toString();
}
out.println(ans);
if(selectedAns.length == 0)
out.println("no elements are selected");
out.println("</body>");
out.println("</body>");

} //end of doget
}
I am getting Err as
500 Internal Server Error
/servlet/CheckBox:
null
java.lang.NullPointerException
at CheckBox.doGet(CheckBox.java, Compiled Code)
thanks,
-padmashree

 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code:
String[] selectedAns = request.getParameterValues("Ans");
is undoubtedly returning a null - if none of the "Ans" checkboxes was checked, the parameter does not appear in the request.
You should ALWAYS check for null before trying to use input parameters.
Bill

------------------
author of:
 
padmshree Patil
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot.
At last ,Checkbox problem was solved
-padmashree
reply
    Bookmark Topic Watch Topic
  • New Topic