Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Jsp Error (CheckBok related..)

 
padmshree Patil
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In my Test.jsp page I have 4 check box for selected correct answers.After selecting I am retriving selected answer as..
String[] selectedAns = request.getParameterValues("Ans");

if (selectedAns.length == 1)
{
String answer = selectedAns[0];
out.println("selected answer : " +answer+ "<P>");
ans=answer;
}
else
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();

}
else
out.println(" nothnig is selected");

------------------------------------
It's working fine if 1 or more checkbox r selected .But if no checkbosx selected then I am getting
NullpointerException !!
How can I check ,if no checkbox r selected?
-
padmashree

 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String[] selectedAns = request.getParameterValues("Ans");
if( selectedAns == null ){
// no checkboxes selected branch
}
Bill
 
padmshree Patil
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this also
if(selecetedAns= null)
{

}
it gives Err as

null
java.lang.NumberFormatException: null
Is there any otherway to solve this ??
 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a minor bit of refinement is needed. SelectedAns is an array ... if no checkboxes were checked then nothing was passed into the array therefore what you need to check is if(selectedAns.length == 0) You were almost there with your original code where you check for values > 0
 
Sowmya Vinay
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by padmshree Patil:
I tried this also
if(selectedAns= null)
{

}
it gives Err as

null
java.lang.NumberFormatException: null
Is there any otherway to solve this ??

You have written as "if(selectedAns= null) " Is this a typo???
If it is, then it should be corrected as "if(selectedAns == null)" just as Bill Brogden mentions, which is the right way of overcoming null pointer exception. Please note (selectedAns.length==0) will not work if selectedAns has null value.

[This message has been edited by Sowmya Vinay (edited February 08, 2001).]
 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oooh good point ... I wasn't really paying attention
 
padmshree Patil
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sowmya Vinay:
Originally posted by padmshree Patil:
[b]I tried this also

You have written as "if(selectedAns= null) " Is this a typo???
If it is, then it should be corrected as "if(selectedAns == null)" just as Bill Brogden mentions, which is the right way of overcoming null pointer exception. Please note (selectedAns.length==0) will not work if selectedAns has null value. ).][/B]


I did tried with if(selectedAns == null)
{
}
Ok .Now I will write simple servlet and I will debug the checkBox problem.if I don't get it then I will bug u people again !!!
-
padmashree
 
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 getting Err as
500 Internal Server Error
/servlet/CheckBox:
null
java.lang.NullPointerException
at CheckBox.doGet(CheckBox.java, Compiled Code)

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic