title: Prime Faces error Cannot add the same component twice
body: <p>I am new to Java Server Faces. I am doing one simple login jsf application which has layout.xhtml, login.xhtml, loginbean.java, changepassword.xhtml, changepasswordbean.java. Login functions are working fine but changepassword function is causing some problem which I can't find what's the reason for error. I am getting error when clicking Clear Button in changepassword.xhtml page. If I click changepassword button a Null pointer exception have occured because I am trying to get a value(companyid) from another loginbean to changepasswordbean. After clicking back button in browser then selecting changepassword menu I am getting a error like Parent was not null, but this component not related. Sometimes menus will not be displayed. I don't know what's the problem, so any help here.</p>
  
  <p>LoginBean.java</p>
  
  <pre><code>    import javax.faces.bean.ManagedBean;
      import javax.faces.bean.ManagedProperty;
      import javax.faces.bean.SessionScoped;
      import java.io.Serializable;
      import javax.faces.application.FacesMessage;
       import javax.faces.context.FacesContext;
      import org.slf4j.Logger;
      import org.slf4j.LoggerFactory;
  
       @ManagedBean
      @SessionScoped
        public class LoginBean implements Serializable 
      {
       Logger log;
  
        @ManagedProperty(value = "loginBean")
  
        public boolean isLoggedin;
       public String username;
         public String password;    
       public String companyid;  
       public boolean notloggedin;   
  
      @ManagedProperty(value = "#{tabMenu}")
      private TabMenu tabMenu;
  
      public LoginBean() 
        {
            log=LoggerFactory.getLogger(LoginBean.class);
             }
  
       public void clear()
          {        
       setUsername(null);
       setPassword(null);
       setCompanyId(null);
      }
  
        public String login()
       {       
      setIsLoggedin(true);
              setNotLoggedIn(false);                
              setCompanyId("companyid_1");          
              tabMenu.setTabMenu();
              return "home";
  
  
   }   
  
  public String logout()
  {
      FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
      setIsLoggedin(false);
      setNotLoggedIn(true);
      return "/login.xhtml?faces-redirect=true";
  }
  
  public void setUsername(String username)
  {
      this.username=username;
  }
  
  public String getUsername()
  {       
      return username;
  }
  
  public void setPassword(String password)
  {
      this.password=password;
  }
  
  public String getPassword()
  {       
      return password;
  }
  
  public void setIsLoggedin(boolean isloggedin)
  {
      this.isLoggedin=isloggedin;
  }
  
  public boolean getIsLoggedin()
  {               
      return isLoggedin;
  }
  
  public void setNotLoggedIn(boolean notloggedin)
  {
      this.notloggedin=notloggedin;
  }
  
  public boolean getNotLoggedIn()
  {       
      if(getIsLoggedin())
      {
          this.notloggedin=false;
      }
      else
          this.notloggedin=true;
  
      return notloggedin;
  }
  
  public void setCompanyId(String companyid)
  {
      this.companyid=companyid;
  }
  
  public String getCompanyId()
  {       
      return companyid;
  }
  
  public TabMenu getTabMenu() 
  {
      return tabMenu;
  }
  
  public void setTabMenu(TabMenu tabMenu)
  {
      this.tabMenu = tabMenu;
  }    
  
      }
  </code></pre>
  
  <p>ChangePasswordBean.java</p>
  
  <pre><code>    import java.io.Serializable;
      import javax.faces.application.FacesMessage;
      import javax.faces.bean.ManagedBean;
       import javax.faces.bean.ManagedProperty;
       import javax.faces.bean.RequestScoped;
       import javax.faces.context.FacesContext;
       import org.slf4j.Logger;
      import org.slf4j.LoggerFactory;
  
     @ManagedBean
       @RequestScoped
        public class ChangePasswordBean implements Serializable
         {
       Logger log;
  
  @ManagedProperty(value = "changePasswordBean")
  public String oldPassword;    
  public String newPassword;
  public String retypePassword;
  
  @ManagedProperty(value = "#{loginBean}")
  public LoginBean lbean;
  
  public ChangePasswordBean() 
  {
      log=LoggerFactory.getLogger(ChangePasswordBean.class);
  }
  
  public void changePassword()
  {
      log.debug("Company Id: "+lbean.getCompanyId());
      log.debug("User Name: "+lbean.getUsername());      
      boolean flag=false;
      ChangePasswordDAO changepass=new ChangePasswordDAO();
  
      if(oldPassword!=null &amp;&amp; newPassword!=null &amp;&amp; retypePassword!=null)
      {
          if(newPassword.equals(retypePassword))
          {
              flag=changepass.changePassword(oldPassword, newPassword,lbean.getUsername(),lbean.getCompanyId());
              if(flag)
              {
                  FacesContext.getCurrentInstance().addMessage("changepassform:btnchange", new FacesMessage(FacesMessage.SEVERITY_INFO,"Info", "Password Changed Successfully"));
              }
              else
              {
                  FacesContext.getCurrentInstance().addMessage("changepassform:btnchange", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Password Not Changed"));
              }
          }
          else
          {
              FacesContext.getCurrentInstance().addMessage("changepassform:btnchange", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "New Password and Retype Password didn't match"));
          }
      }
      else
      {
          FacesContext.getCurrentInstance().addMessage("changepassform:btnchange", new FacesMessage(FacesMessage.SEVERITY_WARN, "Warning", "Old Password/New Password/Retype Password Should not be empty"));
      }
  
  }
  
  public void clear()
  {        
      setOldPassword(null);
      setNewPassword(null);
      setRetypePassword(null);
  }
  
  public void setOldPassword(String oldPassword)
  {
      this.oldPassword=oldPassword;
  }
  
  public String getOldPassword()
  {
      return oldPassword;
  }
  
  public void setNewPassword(String newPassword)
  {
      this.newPassword=newPassword;
  }
  
  public String getNewPassword()
  {
      return newPassword;
  }
  
  public void setRetypePassword(String retypePassword)
  {
      this.retypePassword=retypePassword;
  }
  
  public String getRetypePassword()
  {
      return retypePassword;
  }
  
  public void setLbean(LoginBean lbean)
  {
      this.lbean=lbean;
  }
  
  public LoginBean getLbean()
  {
      return lbean;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>changepassword.xhtml</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;!DOCTYPE html&gt;
      &lt;html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
       xmlns:p="http://primefaces.org/ui"&gt;    
      &lt;body&gt;
      &lt;ui:composition template="../templates/layout.xhtml"&gt;
          &lt;ui:define name="content"&gt;
              &lt;h:form id="changepassform" rendered="#{loginBean.isLoggedin}"&gt;
                  &lt;p:messages id="messages" autoUpdate="true" redisplay="false" 
       showDetail="true"/&gt;
                  &lt;h:panelGrid columns="2"&gt;
                      &lt;h:outputLabel value="Current Password"/&gt;
                      &lt;p:inputText value="#{changePasswordBean.oldPassword}" style="width: 106px;"/&gt;
                      &lt;h:outputLabel value="New Password"/&gt;
                      &lt;p:password value="#{changePasswordBean.newPassword}" style="width: 106px;"&gt;&lt;/p:password&gt;
                      &lt;h:outputLabel value="Retype New Password"/&gt;
                      &lt;p:inputText value="#{changePasswordBean.retypePassword}" style="width: 106px;"/&gt;                        
                  &lt;/h:panelGrid&gt;
                  &lt;br&gt;&lt;/br&gt;
                  &lt;h:panelGrid columns="2" style="margin-left: 100px"&gt;
                      &lt;h:commandButton action="#{changePasswordBean.changePassword()}" value="Change Password" id="btnchange" /&gt;
                      &lt;h:commandButton action="#{changePasswordBean.clear()}" value="Clear" id="btnclear" /&gt;
                  &lt;/h:panelGrid&gt;
              &lt;/h:form&gt;
          &lt;/ui:define&gt;
      &lt;/ui:composition&gt;
  &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>