title: Strange behavior if statement in Servlet
body: <p>I'm facing a very strange problem, it's supposed to be a very easy problem but I'm running around in circles since 2 days.
  I am setting up a login form, there are 3 types of users: Admin, Employee and user.
  I created a simple &quot;if else&quot; statement. In the first &quot;if&quot; I check if the user is an admin then I display the page, in the second if it is an employee and in the third if it is a user.
  It works without problem for the first two &quot;if&quot; (admin and employee) but not for the user.</p>
  <pre><code>private void connexion(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          
          employee.setLogin(request.getParameter(&quot;login&quot;));
          employee.setPassword(request.getParameter(&quot;password&quot;));
          user.setLogin(request.getParameter(&quot;login&quot;));
          user.setPassword(request.getParameter(&quot;password&quot;));
         
           
          User user1 = new User();
          Employee employee1 = new Employee();
          
          user1 = user.findUser(request.getParameter(&quot;login&quot;), request.getParameter(&quot;password&quot;));
          employee1 = employee.findEmployee(request.getParameter(&quot;login&quot;), request.getParameter(&quot;password&quot;));
          
              
           if (employee1 != null &amp;&amp; &quot;Employee&quot;.equals(employee1.getStatus().getStatus()))
           {
          request.getSession().setAttribute(&quot;employee&quot;, employee1);
            this.getServletContext().getRequestDispatcher(&quot;/employee.jsp&quot;).forward(request, response);--&gt;Ok
          }
           else if(employee1 != null &amp;&amp; &quot;Admin&quot;.equals(employee1.getStatus().getStatus())) 
           {
              request.getSession().setAttribute(&quot;employee&quot;, employee1);
              this.getServletContext().getRequestDispatcher(&quot;/admin.jsp&quot;).forward(request, response);
          }
           else if (user1 != null &amp;&amp; &quot;User&quot;.equals(user1.getStatus().getStatus())) 
           {
              request.getSession().setAttribute(&quot;user&quot;, user1);
              this.getServletContext().getRequestDispatcher(&quot;/user.jsp&quot;).forward(request, response);--&gt; Not working here.
          }
          
           
      }
  </code></pre>
  <p>But if I put the user in the first place, it works for the user but not for the other two. really strange, I hope you understood lol</p>
  <pre><code>    if (user1 != null &amp;&amp; &quot;User&quot;.equals(user1.getStatus().getStatus())) 
      {
      request.getSession().setAttribute(&quot;user&quot;, user1);
      this.getServletContext().getRequestDispatcher(&quot;/succ.jsp&quot;).forward(request, response);--&gt; Now it's working
     }
      
      else if (employee1 != null &amp;&amp; &quot;Employee&quot;.equals(employee1.getStatus().getStatus()))
       {
          request.getSession().setAttribute(&quot;employee&quot;, employee1);
          this.getServletContext().getRequestDispatcher(&quot;/employee.jsp&quot;).forward(request, response);--&gt;doesn't work
      }
       else if(employee1 != null &amp;&amp; &quot;Admin&quot;.equals(employee1.getStatus().getStatus())) 
       {
          request.getSession().setAttribute(&quot;employee&quot;, employee1);
          this.getServletContext().getRequestDispatcher(&quot;/admin.jsp&quot;).forward(request, response);--&gt;Doesn't work
      }
       
  </code></pre>
  <p>Here is the error message I got</p>
  <pre><code>java.lang.NullPointerException: Cannot invoke &quot;model.Status.getStatus()&quot; because the return value of &quot;model.Employee.getStatus()&quot; is null
  </code></pre>
  <p>I debbuged and it's not true, getStatus is not null...</p>
  <p>The jsp is a simple form</p>
  <pre><code>&lt;form method=&quot;post&quot; action=&quot;connexion&quot;&gt; 
                          &lt;h2&gt;Connexion&lt;/h2&gt;
                                      
                      &lt;fieldset class=&quot;form-group&quot;&gt;
                          &lt;label&gt;Login &lt;/label&gt;
                          &lt;input type=&quot;text&quot; id=&quot;login&quot; name=&quot;login&quot; class=&quot;form-control&quot; required=&quot;required&quot;/&gt;
                          &lt;span class=&quot;erreur&quot;&gt;${form.errors['firstName']}&lt;/span&gt;
                      &lt;/fieldset&gt;
                      &lt;fieldset class=&quot;form-group&quot;&gt;
                          &lt;label&gt;password&lt;/label&gt;
                          &lt;input type=&quot;password&quot; id=&quot;password&quot; name=&quot;password&quot; class=&quot;form-control&quot; required=&quot;required&quot;/&gt;
                          &lt;span class=&quot;erreur&quot;&gt;${form.errors['lastName']}&lt;/span&gt;
                      &lt;/fieldset&gt;                                     
                      
                      &lt;button type=&quot;submit&quot; name=&quot;action&quot; value=&quot;Ouvrir session&quot; class=&quot;btn btn-success&quot;&gt;Save&lt;/button&gt;
  
                      
                  &lt;/form&gt;
  </code></pre>
  <p>findEmployee and findUser :</p>
  <pre><code>@Override
      public Employee findEmployee(String login, String password) {
          Connection connection = null;
          PreparedStatement ps = null;
          ResultSet rs = null;    
          Employee employee = new Employee();
          Status status = new Status();
  
          
          try {
              connection = MysqlDaoFactory.getInstance().getConnection();
              ps = connection.prepareStatement(SQL_LOGIN);
              ps.setString(1, login );
              ps.setString(2, password);
              rs = ps.executeQuery();
              if(rs.next()) {
                  employee.setId(rs.getInt(&quot;id&quot;));
                  employee.setFirstName(rs.getString(&quot;firstName&quot;));
                  employee.setLastName(rs.getString(&quot;lastName&quot;));
                  employee.setEmail(rs.getString(&quot;email&quot;));
                  employee.setAddress(rs.getString(&quot;address&quot;));
                  employee.setLogin(rs.getString(&quot;login&quot;));
                  employee.setPassword(rs.getString(&quot;password&quot;));
                  employee.setActive(rs.getBoolean(&quot;active&quot;));
                  employee.setStatus(status.findbyId(rs.getInt(&quot;statusId&quot;)));
  
  
              }
          }catch(SQLException e) {
              e.printStackTrace();
          }finally {
               MysqlConnectionFactory.closeConnection(connection);
                  MysqlConnectionFactory.closeResultSet(rs);
              }
              return employee;
          }
  
  @Override
      public User findUser(String login, String password)
      {
          
          Connection connection = null;
          PreparedStatement ps = null;
          ResultSet rs = null;
          User user = new User();
          Status status = new Status();
          Country country = new Country();
  
          try {
              connection = MysqlDaoFactory.getInstance().getConnection();
              ps = connection.prepareStatement(SQL_LOGIN);
              ps.setString(1, login);
              ps.setString(2, password);
              rs = ps.executeQuery();
              if(rs.next()) {
                  user.setId(rs.getInt(&quot;id&quot;));
                  user.setLastName(rs.getString(&quot;lastName&quot;));
                  user.setFirstName(rs.getString(&quot;firstName&quot;));
                  user.setBirthDate(rs.getDate(&quot;birthDate&quot;));
                  user.setEmail(rs.getString(&quot;email&quot;));
                  user.setAddress(rs.getString(&quot;address&quot;));
                  user.setZipCode(rs.getString(&quot;zipCode&quot;));
                  user.setCity(rs.getString(&quot;city&quot;));
                  user.setPhone(rs.getString(&quot;phone&quot;));
                  user.setLogin(rs.getString(&quot;login&quot;));
                  user.setPassword(rs.getString(&quot;password&quot;));
                  user.setActive(rs.getBoolean(&quot;active&quot;));
                  user.setSolde(rs.getDouble(&quot;solde&quot;));
                  user.setCountry(country.findById(rs.getInt(&quot;countryId&quot;)));
                  user.setStatus(status.findbyId(rs.getInt(&quot;statusId&quot;)));
          
              }
              
          }catch (SQLException e) 
          {
              e.printStackTrace();
          }
          
          finally
          {
              MysqlConnectionFactory.closePreparedStatement(ps);
              MysqlConnectionFactory.closeResultSet(rs);
              MysqlConnectionFactory.closeConnection(connection); 
          }
              
          return user;
      }
              
  </code></pre>