rags
rags

Reputation: 2590

Prime faces CRUD Operations

I am trying to do Insert Operation in a table. I display exiting records in a datatable and details are displayed in a panelGrid below the dataTable on selecting each row. panelGrid with blank inputText boxes are shown when user click NEW button. User Submits the new record and dataTable is refreshed.

On submit, I am getting error :

Nov 22, 2011 5:02:22 PM com.sun.faces.lifecycle.ProcessValidationsPhase execute WARNING: Argument Error: Parameter targetClass is null java.lang.NullPointerException: Argument Error: Parameter targetClass is null

Code is Given below:

<p:outputPanel header="MyTable Records" rendered="true" id="panel_MyTable">         
    <p:dataTable id="table_MyTable" value="#{myBean.records}" var="dataMyTable" onRowSelectUpdate="details_MyTable" selection="#{myBean.currentRec}" update="submitButton">
         <p:column>
               <f:facet name="header">
                    <h:outputLabel value="Label 1" />
               </f:facet>
               <h:outputLabel value="#{dataMyTable.Field1}"/>
         </p:column>

         <p:column>
             <f:facet name="header">
                 <h:outputLabel value="Label 2" />
             </f:facet>
             <h:outputLabel value="#{dataMyTable.Field2}"/>
         </p:column>

         <f:facet name="footer">
               <p:commandButton value="New"  image="ui-icon ui-icon-add" actionListener="#{myBean.prepareForInsertAction}" update="details_myTable,submitButton" />
         </f:facet>
  </p:dataTable>
</p:outputPanel>



<h:panelGrid id="details_MyTable">
    <h:outputLabel value="Label 1"/>
        <p:inputText readonly="#{myBean.editMode ? false : true}" value="#{myBean.editMode ? myBean.newRec.Field1 : myBean.currentRec.Field1}" />
        <h:outputLabel value="Label 2"/>
        <p:inputText readonly="#{myBean.editMode ? false : true}" value="#{myBean.editMode ? myBean.newRec.Field2 : myBean.currentRec.Field2}" />
</h:panelGrid>

<p:commandButton id="submitButton" actionListener="#{myBean.createAction}" value="Submit" update="table_MyTable,details_MyTable" rendered="#{myBean.editMode ? true : false}"/>




@ManagedBean(name="myBean")
@ViewScoped
public class MyBean
{
    public static List<MYTABLE> records;
    MYTABLE currentRec;
    Boolean editMode=false;

    public MyBean(){
        records = MYTABLE_CRUD.getAllRecs();
        currentRec = new MYTABLE();
    }

    public void prepareForInsertAction(){
        newRec = new MYTABLE();
        editMode = true;
    }
    public void setCurrentRec(MYTABLE v_currentRec) {
        this.currentRec = v_currentRec;
        editMode = false;
    }

}

Upvotes: 1

Views: 2420

Answers (2)

BalusC
BalusC

Reputation: 1109432

Those value attributes are not right:

<p:inputText readonly="#{myBean.editMode ? false : true}" value="#{myBean.editMode ? myBean.newRec.Field1 : myBean.currentRec.Field1}" />
<p:inputText readonly="#{myBean.editMode ? false : true}" value="#{myBean.editMode ? myBean.newRec.Field2 : myBean.currentRec.Field2}" />

Those values can be retrieved, but new values cannot be set. The following syntax will work:

<p:inputText readonly="#{!myBean.editMode}" value="#{myBean[myBean.editMode ? 'newRec' : 'currentRec'].Field1}" />
<p:inputText readonly="#{!myBean.editMode}" value="#{myBean[myBean.editMode ? 'newRec' : 'currentRec'].Field2}" />

(note that I improved the readonly attribute evaluation as well)

But much better is to use just one and same property for both edit modes and use the edit mode value in the action method to determine how to deal with it:

<p:inputText readonly="#{!myBean.editMode}" value="#{myBean.rec.Field1}" />
<p:inputText readonly="#{!myBean.editMode}" value="#{myBean.rec.Field2}" />

with e.g.

private Record rec;

public void save() {
    if (editMode) {
        // Treat "rec" as "currentRec".
    } else {
        // Treat "rec" as "newRec".
    }
}

Upvotes: 2

rags
rags

Reputation: 2590

Having Single panelGrid and using it for viewing table row data and also for accepting new record details from user based on the dynamic binding on the values of inputTexts in panelGrid is causing the problem. So, I started using two panelGrids, one for viewing the existing table row data and another for accepting the new record data from user.

Upvotes: 1

Related Questions