I am developing an online quiz system in JSP.There are 4 radio buttons for each question. When none of the buttons are checked and submitted,it shows NullPointerException. How to overcome it?
Asked
Active
Viewed 35 times
-1
-
First, there is a great post about NPE and then, how could we help with only this... a [mcve] is need for those kind of question – AxelH Apr 20 '17 at 12:19
1 Answers
1
There are two approaches to overcome it.
1) If it is compulsory for user to select an option, on the client side you can use javascript or jquery to enforce that. You can refer to How can I check whether a radio button is selected with JavaScript? and Adding validations to see if radio button is not selected
2) If it is optional for user to select an option, on the server side you can check whether the value is null or not before performing operation on it. Assuming you are using jsp or servlet, you can write something like this:
String answer = request.getParameter("YourRadioName");
if(answer != null) {
//do some operation here
}