title: servlet is not working (also has null pointer error) , servlet couldn't get the value from javascript
body: <p>I couldn't  find a reason why servlet is not working here.</p>
  
  <p>So, in html, i have a select country field, empty first. Once i click the drop down menu to choose nothing, and then click anywhere else, it should show the error message(Please choose a country) next to the field.</p>
  
  <p>html:</p>
  
  <pre><code>&lt;select id="select-country" name = "select-country/region" onblur="validateCountry()"&gt;
                          &lt;option value = "" selected = "selected"&gt;Select One&lt;/option&gt;
                          &lt;option value ="AU"&gt;Australia&lt;/option&gt;
                          &lt;option value ="BA"&gt;Bahamas&lt;/option&gt;
                          &lt;option value ="CN"&gt;China&lt;/option&gt;
                          &lt;option value ="DK"&gt;Denmark&lt;/option&gt;
                          &lt;option value ="US"&gt;United States&lt;/option&gt;        
                      &lt;/select&gt;                    &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
                      &lt;span id = "countryError"&gt;&lt;/span&gt;
  </code></pre>
  
  <p>javascript:</p>
  
  <pre><code>var req;
  var f;
  var t;
  
      function validateCountry()
      {
          t = document.getElementById("select-country");
          f = document.getElementById("countryError");
          alert ("start here" + t.value);
          var url = "http://localhost:8080/ibmProject/servlet?select-country=" + t.value();
          alert ("Hi,here is inside validateCountry");
          req = new XMLHttpRequest();
          req.open("GET", url, true);
          req.onreadystatechange = callback;
          req.send(null);     
      }
      function callback()
      {
          alert ("Hi,here is inside countryValidation");
          if (req.readyState === 4 &amp;&amp; req.status === 200)
          {
              var return_data = req.responseText;
              alert("callback: " + return_data);
              f.innerHTML = return_data;
              f.style.color = "#FF0000";
              t.focus();   
          }
      }
  </code></pre>
  
  <p>servlet:</p>
  
  <p>In servlet, if i don't add <code>if(c!=null)</code> condition, it gives the null pointer error. I tried to type <a href="http://localhost:8080/ibmProject/servlet" rel="nofollow">http://localhost:8080/ibmProject/servlet</a> in the browser to test if servlet is working or not. Same thing, if i don't add <code>if(c!=null)</code>, it gives HTTP Status 500 - java.lang.NullPointerException.</p>
  
  <p>However, no matter what i choose in the country field, the string c in servlet is always null. 
  Also, even if the string c is null, then,  <code>out.print("*testing!*")</code> should be returned to <code>var return_data = req.responseText</code> (in javascript), but it didn't. There is no error message :"testing" next to country field. </p>
  
  <p>I checked the apache folder, </p>
  
  <blockquote>
    <p>C:\apache-tomcat-7.0.68\work\Catalina\localhost/ibmProject</p>
  </blockquote>
  
  <p>it is empty, does it suppose to have some files? </p>
  
  <p>I realized that <code>alert ("Hi,here is inside validateCountry")</code>; (javascript) never executes.<br>
  So, i guess the problem is about 
  <code>var url = "http://localhost:8080/ibmProject/servlet?select-country=" + t.value();</code> (in javascript)
  this line. 
  The servlet.java is located here.
  NetBeansProjects\ibmProject\src\java</p>
  
  <p>The html, javascript, css are all here:
  \NetBeansProjects\ibmProject\web</p>
  
  <pre><code>public class servlet extends HttpServlet {
  
      protected void processRequest(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException 
      {
          response.setContentType("text/plain;charset=UTF-8");
          //response.setHeader("Access-Control-Allow-Origin", "*");
          PrintWriter out = response.getWriter();
          String c = request.getParameter("select-country");
          if(c!=null)
          {
              System.out.println("Finally!!!!!!!!!!!!!!!!!!!!------------------------------------"+c);
              if (c.equals(""))
              {
                  out.print("*Please select a country!*");
              }  
              else 
              {
                  out.print("");
              }
          }
          else
          {
              out.print("*testing...!*");
          }
      }
  </code></pre>
  
  <p><a href="http://i.stack.imgur.com/WCfVt.png" rel="nofollow">pic1</a></p>
  
  <p>web.xml:</p>
  
  <pre><code>&lt;servlet&gt;
          &lt;servlet-name&gt;servlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;servlet&lt;/servlet-class&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;servlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/servlet&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;session-config&gt;
          &lt;session-timeout&gt;
              30
          &lt;/session-timeout&gt;
      &lt;/session-config&gt;
  </code></pre>
  
  <p>Can someone help?
  Really appreciate. </p>