This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Checkbox err..

 
padmshree Patil
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • 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

 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks a lot.
At last ,Checkbox problem was solved
-padmashree
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic