title: Primefaces Datatable Selection Object
body: <p>I have a question to the Primefaces Datatable, especially to the Selection Object.</p>
  
  <p>In my following Code I get always Null for the Variable "Selected Question" which is bound to the Datatable with Selection.</p>
  
  <p>The jsf as followed:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE html
          PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:p="http://primefaces.org/ui"
        xmlns:ui="http://java.sun.com/jsf/facelets"&gt;
  
      &lt;ui:composition template="mainTemplate.xhtml"&gt;
          &lt;ui:define name="contentTitle"&gt;Your Questions&lt;/ui:define&gt;
          &lt;ui:define name="content"&gt;
              &lt;h:form id="formAllQuestion"&gt;
                  &lt;p:growl id="allQuestionGrowl" showDetail="true"/&gt;
  
                  &lt;p:dataTable id="allQuestionsTable" var="question" value="#{allQuestionBean.allQuestionDataHelper}" paginator="true" rows="10"
                              selection="#{allQuestionBean.selectedQuestion}" selectionMode="single"&gt;
  
                      &lt;p:ajax event="rowSelect" listener="#{allQuestionBean.onRowSelect}" update=":formAllQuestion:AnswerToQuestionDialogTable :formAllQuestion:allQuestionGrowl"
                              oncomplete="questDialog.show()"/&gt;
                      &lt;p:ajax event="rowUnselect" listener="#{allQuestionBean.onRowUnselect}" update=":formAllQuestion:allQuestionGrowl"/&gt;
  
  
                      &lt;f:facet name="header"&gt;Select a Row to display your Question Details&lt;/f:facet&gt;
  
                      &lt;p:column headerText="QuestionID"&gt;
                          #{question.questionId}
                      &lt;/p:column&gt;
                      &lt;p:column headerText="Question Name"&gt;
                          #{question.questionName}
                      &lt;/p:column&gt;
                      &lt;p:column headerText="Question Description"&gt;
                          #{question.questionText}
                      &lt;/p:column&gt;
                      &lt;p:column headerText="Question Short Description"&gt;
                          #{question.questionShortText}
                      &lt;/p:column&gt;
                      &lt;p:column headerText="Author"&gt;
                          #{question.professor.profSurename} #{question.professor.profName}
                      &lt;/p:column&gt;
                  &lt;/p:dataTable&gt;
  
  
                  &lt;p:dialog header="Question Details" widgetVar="questionDialog" resizable="true" id="questDialog"
                            showEffect="fade" hideEffect="fade" modal="true"&gt;
                      &lt;p:dataTable id="AnswerToQuestionDialogTable" var="answer" value="#{allQuestionBean.answers}"&gt;
  
                          &lt;f:facet name="header"&gt;
                              Hier kommt der QR_Code rein!
  
                              #{allQuestionBean.selectedQuestion.questionId} - #{allQuestionBean.selectedQuestion.questionName}
                          &lt;/f:facet&gt;
  
                          &lt;p:column headerText="Answer"&gt;
                              &lt;h:outputText value="#{answer.answerText}"/&gt;
                          &lt;/p:column&gt;
                          &lt;p:column headerText="Counts For this Answer"&gt;
                              &lt;h:outputText value="Bis jetz noch nix!"/&gt;
                          &lt;/p:column&gt;
                      &lt;/p:dataTable&gt;
                  &lt;/p:dialog&gt;
  
  
              &lt;/h:form&gt;
          &lt;/ui:define&gt;
      &lt;/ui:composition&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>And the associated Bean Class (AllQuestionBean.class):</p>
  
  <pre><code> @ManagedBean(name = "allQuestionBean")
      @ViewScoped
      public class AllQuestionBean implements Serializable {
  
  
      private static final long serialVersionUID = 7038894302985973905L;
  
      @ManagedProperty(value = "#{questionDAO}")
      private QuestionDAO questionDAO;
  
      @ManagedProperty(value = "#{profSession.professor}")
      private Professor professor;
  
      @ManagedProperty(value = "#{answerDAO}")
      private AnswerDAO answerDAO;
  
      @ManagedProperty(value = "#{answeredDAO}")
      private AnsweredDAO answeredDAO;
  
  
      private List&lt;Question&gt; questions;
      private Question selectedQuestion;
      private List&lt;Answer&gt; answers;
      private AllQuestionDataHelper allQuestionDataHelper;
  
  
      public AllQuestionBean(){
          System.out.println("Starting Bean: "+this.getClass().getName());
      }
  
      @PostConstruct
      public void initVariables(){
          questions = questionDAO.readByProfessor(professor);
      }
  
      public void onRowSelect(SelectEvent event) {
  
  
  
          FacesMessage msg = new FacesMessage("Question Selected", selectedQuestion.getQuestionId()+" -- "+selectedQuestion.getQuestionName());
  
          FacesContext.getCurrentInstance().addMessage(null, msg);
      }
  
      public void onRowUnselect(UnselectEvent event) {
  
          FacesMessage msg = new FacesMessage("Question Selected", selectedQuestion.getQuestionId()+" -- "+selectedQuestion.getQuestionName());
  
          FacesContext.getCurrentInstance().addMessage(null, msg);
      }
  
  
      //---GETTER and SETTER
  
      public AllQuestionDataHelper getAllQuestionDataHelper() {
          allQuestionDataHelper = new AllQuestionDataHelper(questions);
          return allQuestionDataHelper;
      }
  
  
      public void setAllQuestionDataHelper(AllQuestionDataHelper allQuestionDataHelper) {
          this.allQuestionDataHelper = allQuestionDataHelper;
      }
  
  
  
      public QuestionDAO getQuestionDAO() {
          return questionDAO;
      }
  
      public void setQuestionDAO(QuestionDAO questionDAO) {
          this.questionDAO = questionDAO;
      }
  
      public Professor getProfessor() {
          return professor;
      }
  
      public void setProfessor(Professor professor) {
          this.professor = professor;
      }
  
      public AnswerDAO getAnswerDAO() {
          return answerDAO;
      }
  
      public void setAnswerDAO(AnswerDAO answerDAO) {
          this.answerDAO = answerDAO;
      }
  
      public AnsweredDAO getAnsweredDAO() {
          return answeredDAO;
      }
  
      public void setAnsweredDAO(AnsweredDAO answeredDAO) {
          this.answeredDAO = answeredDAO;
      }
  
      public List&lt;Question&gt; getQuestions() {
          return questions;
      }
  
      public void setQuestions(List&lt;Question&gt; questions) {
          this.questions = questions;
      }
  
      public Question getSelectedQuestion() {
          System.out.println("getSelectedQuestion");
          return selectedQuestion;
      }
  
      public void setSelectedQuestion(Question selectedQuestion) {
          System.out.println("Set selected Question: "+selectedQuestion);
          this.selectedQuestion = selectedQuestion;
      }
  
      public List&lt;Answer&gt; getAnswers() {
          answers = answerDAO.getAllAnswersForQuestion(selectedQuestion);
          return answers;
      }
  
      public void setAnswers(List&lt;Answer&gt; answers) {
          this.answers = answers;
      }
      }
  </code></pre>
  
  <p>The Data Modell:</p>
  
  <pre><code>public class AllQuestionDataHelper extends ListDataModel&lt;Question&gt; implements SelectableDataModel&lt;Question&gt; {
  
  
      public AllQuestionDataHelper() {
      }
  
      public AllQuestionDataHelper(List&lt;Question&gt; list) {
          super(list);
      }
  
      @Override
      public Object getRowKey(Question question) {
          if(!(question == null)){
          System.out.println("Your Questions --&gt; Getting RowKey");
          System.out.println("RowKey: "+question);
          System.out.println("RowKey: "+question.getQuestionId());
          }else{
              System.out.println("Warning Row Key is null");
          }
          return question.getQuestionId();
      }
  
      @Override
      public Question getRowData(String rowKey) {
          System.out.println("Your Questions --&gt; Getting RowData");
          System.out.println("RowData: "+rowKey);
  
          List&lt;Question&gt; questionList = (List&lt;Question&gt;) getWrappedData();
  
          for(Question q : questionList){
              if(rowKey.equals(q.getQuestionId())){
                  System.out.println("Returning "+q.getQuestionId());
                  return q;
              }
          }
          return null;
      }
  }
  </code></pre>
  
  <p>I debugged a few runs and mentioned that the Variable "selectedQuestion" in AllQuestionBean.class is never set. Will say, the event Variable in "onRowSelect" holds a NULL-Object.
  As you can see there are two Datatables in the *.xhtml. The first one will load normally, no problems. The onClick-Method of the Bean should launch a Dialog with the Second Datatable, but will quit with a Nullpointer.</p>
  
  <p>For the Datatable I followed the Tutorial at Primefaces (http://www.primefaces.org/showcase-labs/ui/datatableRowSelectionInstant.jsf)</p>