2

I am working on a project in which I need to create a form with a dropdown field for category. And according to the category selected, I have to populate the second drop down called subcaste. This I am achieving through AJAX.

I also wrote a method which will be called on change of the category to disable the sub caste dropdown box if the selected category is OPEN as:

if(str=="OPEN"||str=="open"){
        document.form.subcaste.disabled=true;
} 

But when I hit the submit button, i get a null pointer exception in the line:

subCaste = request.getParameter("subcaste");

in the servlet. (This line takes the value of the subcaste from the jsp page).

I have also done: <option value="none" selected="selected">Select</option> in the drop down of the subcaste so that a default value is selected. But I still get a null pointer exception. I believe that after I disable the dropdown box, the value isnt available to the servlet at all.

The detailed code is:

JSP:

<td id='category'><select name='category' onchange="showSubCaste(this.value);">  
<option value="none" selected="selected">Select</option>  
<% for (i = 0; i < categorySize; i++) {%>
<% category = (String) categoryArr.get(i);%>
<option  value=<%= category%>><%= category%></option>
<% }%>
</select>
</td>
<td >SubCaste</td>
<td id='subcaste'> <select name='subcaste'>  
<option value="none">Select</option>
</select>       
</td>

JavaScript:

function showSubCaste(str){
...
if(str=="OPEN"||str=="open"){
document.form.subcaste.disabled=true;
document.form.issuingAuthority.disabled=true;  
}
else{
document.form.subcaste.disabled=false;
document.form.issuingAuthority.disabled=false;  
var url="SubCasteController";
url +="?caste=" +str;
...}

After retrieving the values in a servlet and passing it to another JSP:

<%String buffer = "<select name='subcaste' onchange='subCasteChanged(this.value);'><option value='none' selected='selected'>Select SubCaste</option>";
for (int i = 0; i < sizeInfo; i++) {
subCaste = (String) retrievedInfo.get(i);
buffer = buffer + "<option value='" + subCaste + "'>" + subCaste + "</option>";
}
buffer = buffer + "</select>";
response.getWriter().println(buffer);
%>

I do not know how to proceed with this. Please help me.

Thank you in advance.

Lavanya Mohan
  • 1,496
  • 7
  • 28
  • 39
  • 2
    You will have to show us the code for the HTML form and more of the neighboring elements of the servlet code for us to have any idea. – jfriend00 Jul 09 '11 at 02:05

1 Answers1

1

Yes , you are right . If the <select> is disabled , its values will not be POSTED. So when you get its value using request.getParameter() , it will return null pointer exception.

The standard practices to get a disabled <select> to post its value are

  • Add a hidden input field that will submit the same value and copy the value from the disabled <select> to this hidden field in the <form> 's onsubmit() event

or

  • Re-enable the disabled <select> in the <form> 's onsubmit() event

Or alternatively , as you believe the null pointer exception is because subCaste is set to null ,you can try to to set subCaste variable to some specific value if the subCaste parameters is null to see if it can be solved.

if (  request.getParameter("subcaste") != null){
    subCaste = request.getParameter("subcaste");
}else{
   subCaste = "xxxxx"; //set to a  specific value according to your requirement. 
}

Reference

HTML form readonly SELECT tag/input

Community
  • 1
  • 1
Ken Chan
  • 84,777
  • 26
  • 143
  • 172
  • yes , i do not mean it will throw NPE there, my point is that because OP said he believes that the NPE is because the subCaste is set to null , so i just suggest he can try to to set subCaste to some specific value if the `subCaste` parameters is null to see if it can be solved.I just revised my post , hope it can express my point of view more clearly. – Ken Chan Jul 09 '11 at 12:54