title: Servlet HTTP 500 - NullPointerException
body: <p>I'm just starting with J2E. </p>
  
  <p>I got <code>NullPointerException</code> in the line:</p>
  
  <pre><code>if (!userName.equals("") &amp;&amp; userName != null)
  </code></pre>
  
  <p>When I run server like this: </p>
  
  <pre><code>http://localhost:8080/SimpleServletProject/SimpleServletPath?name=test
  </code></pre>
  
  <p>It works perfectly. but not accepting null.</p>
  
  <p>Whole code :</p>
  
  <pre><code>package org.mojservlet;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  
  import javax.servlet.ServletContext;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  
  @WebServlet("/SimpleServletPath")
  public class SimpleServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  
  
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      System.out.println("get method");
      response.setContentType("text/html");
      PrintWriter writer = response.getWriter();
      String userName = request.getParameter("name");
      HttpSession session = request.getSession();
      ServletContext context = request.getServletContext();
      if (!userName.equals("") &amp;&amp; userName != null){ 
          session.setAttribute("savedUserName", userName);
          context.setAttribute("savedUserName", userName);
      }
      writer.println("Request parameter has username as " + userName);
      writer.println("     Session paramter has username as " + (String) session.getAttribute("savedUserName"));
      writer.println("     context is " + (String) context.getAttribute(userName));
  }}
  </code></pre>