我正在尝试在primefaces datatable上执行行编辑。我从数据库中获取数据表的所有值。在onEdit方法中,我只是尝试获取正在编辑的行的主键值。当我尝试编辑时或点击刻度线或十字标记取消编辑我得到以下异常。我是JSF的新手,请帮助如何进行行编辑,可以将值更新到数据库。
我得到的例外
2013年8月22日上午3:56:56 com.sun.faces.context.PartialViewContextImpl processPartial 信息:java.lang.NullPointerException 显示java.lang.NullPointerException at org.primefaces.component.datatable.feature.SelectionFeature.decodeMultipleSelection(SelectionFeature.java:51) at org.primefaces.component.datatable.feature.SelectionFeature.decode(SelectionFeature.java:40) at org.primefaces.component.datatable.DataTableRenderer.decode(DataTableRenderer.java:57) 在javax.faces.component.UIComponentBase.decode(UIComponentBase.java:787) 在org.primefaces.component.api.UIData.processDecodes(UIData.java:228) at com.sun.faces.context.PartialViewContextImpl $ PhaseAwareVisitCallback.visit(PartialViewContextImpl.java:507) at com.sun.faces.component.visit.PartialVisitContext.invokeVisitCallback(PartialVisitContext.java:183) 在org.primefaces.component.api.UIData.visitTree(UIData.java:639) 在javax.faces.component.UIComponent.visitTree(UIComponent.java:1623) 在javax.faces.component.UIForm.visitTree(UIForm.java:371) 在javax.faces.component.UIComponent.visitTree(UIComponent.java:1623) 在javax.faces.component.UIComponent.visitTree(UIComponent.java:1623) at com.sun.faces.context.PartialViewContextImpl.processComponents(PartialViewContextImpl.java:377) at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:252) 在javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:183) 在javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:931) at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78) 在com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 在com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 在javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 在org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) 在org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:602) 在org.apache.tomcat.util.net.JIoEndpoint $ Worker.run(JIoEndpoint.java:489) 在java.lang.Thread.run(未知来源)
我的JSF代码段
<h:form id="lpcForm">
<div id="content">
<p:dataTable var="lpcData" value="#{lpcBean.lpcIdList}" id="lpcList"
editable="true" scrollable="true" scrollWidth="1110"
tableStyle="table-layout:auto; width:1130px;" scrollHeight="330"
styleClass="datatable">
<p:ajax event="rowEdit" listener="#{lpcBean.onEdit}" />
<p:ajax event="rowEditCancel" listener="#{lpcBean.onCancel}" />
<p:column selectionMode="multiple" style="width:5%" />
<p:column headerText="LPC ID" style="width:20%">
<h:outputText value="#{lpcData.LPCID}" />
</p:column>
<p:column headerText="First Name" style="width:20%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{lpcData.name_First}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{lpcData.name_First}" style="width:100%"
label="firstName" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Last Name" style="width:24%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{lpcData.name_Last}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{lpcData.name_Last}" style="width:100%"
label="lastName" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Email" style="width:20%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{lpcData.email_Address}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{lpcData.email_Address}" style="width:100%"
label="emailAddress" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Region" style="width:24%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{lpcData.region}" />
</f:facet>
<f:facet name="input">
<h:selectOneMenu value="#{lpcData.region}">
<f:selectItems value="#{lpcBean.regions}" />
</h:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="State" style="width:24%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{lpcData.homeState}" />
</f:facet>
<f:facet name="input">
<h:selectOneMenu value="#{lpcData.homeState}">
<f:selectItems value="#{lpcBean.mainStates}" />
</h:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Profit Center" style="width:20%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{lpcData.profitCenter}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{lpcData.profitCenter}" style="width:100%"
label="profitCenter" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Active" style="width:24%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{lpcData.active_LPC}" />
</f:facet>
<f:facet name="input">
<h:selectOneMenu value="#{lpcData.active_LPC}">
<f:selectItems value="#{lpcBean.activeLpcs}" />
</h:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column style="width:6%">
<p:rowEditor />
</p:column>
</p:dataTable>
</h:form>
My Managed bean snippet
@RequestScoped
@ManagedBean
public class LpcBean implements Serializable {
private List<LPC>lpcItems=new ArrayList<LPC>();
public static LPC[] selectedRows;
public static LPC itemSelect;
private LpcDataModel lpcIdList;
Connection connection;
Statement selectStmt1,selectStmt2,selectStmt3,selectStmt4;
private List<String> nameList;
private String selectedLpcName;
private String selectedItem;
private List<String> regions=new ArrayList<String>();
private List<String>mainStates=new ArrayList<String>();
private List<String>activeLpcs=new ArrayList<String>();
public LpcBean() {
System.out.println("inside the constructor");
lpcIdList = new LpcDataModel(lpcItems);
getLpcFields();
}
public List<LPC> getLpcItems() {
return lpcItems;
}
public void setLpcItems(List<LPC> lpcItems) {
this.lpcItems = lpcItems;
}
public LPC[] getSelectedRows() {
return selectedRows;
}
public void setSelectedRows(LPC[] selectedRows) {
LpcBean.selectedRows = selectedRows;
}
public LpcDataModel getLpcIdList() {
return lpcIdList;
}
public void setLpcIdList(LpcDataModel lpcIdList) {
this.lpcIdList = lpcIdList;
}
public String getSelectedItem() {
return selectedItem;
}
public void setSelectedItem(String selectedItem) {
this.selectedItem = selectedItem;
}
public List<String> getNameList() {
return nameList;
}
public void setNameList(List<String> nameList) {
this.nameList = nameList;
}
public String getSelectedLpcName() {
return selectedLpcName;
}
public void setSelectedLpcName(String selectedLpcName) {
this.selectedLpcName = selectedLpcName;
}
public void getLpcFields() {
try
{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:jtds:sqlserver://cvgapp106I/dev2_LPSR");
System.out.println("connected to the database");
selectStmt1 = connection.createStatement();
ResultSet rs1 = selectStmt1.executeQuery("select * from LPC order by LPCID");
while(rs1.next()){
lpcItems.add(new LPC(rs1.getString("LPCID"),rs1.getString("Name_First"),
rs1.getString("Name_Last"),rs1.getString("Email_Address"),rs1.getString("Region"),
rs1.getString("HomeState"),rs1.getString("ProfitCenter"),rs1.getString("Active_LPC")));
}
selectStmt2=connection.createStatement();
ResultSet rs2=selectStmt2.executeQuery("Select * from Tbl_state");
while(rs2.next())
{
mainStates.add(rs2.getString("State"));
}
selectStmt3=connection.createStatement();
ResultSet rs3=selectStmt3.executeQuery("Select * from Tbl_Regional_Managers");
while(rs3.next())
{
regions.add(rs3.getString("Location"));
}
selectStmt4=connection.createStatement();
ResultSet rs4=selectStmt4.executeQuery("Select * from YesNo");
while(rs4.next())
{
activeLpcs.add(rs4.getString("YesNo_Key"));
}
}
catch (Exception e)
{
//System.out.println("Error Data : " + e.getMessage());
e.printStackTrace();
}
finally
{
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void setRegions(List<String> regions) {
this.regions = regions;
}
public void setMainStates(List<String> mainStates) {
this.mainStates = mainStates;
}
public void setActiveLpcs(List<String> activeLpcs) {
this.activeLpcs = activeLpcs;
}
public List<String> getRegions() {
return regions;
}
public List<String> getMainStates() {
return mainStates;
}
public List<String> getActiveLpcs() {
return activeLpcs;
}
public void onEdit(RowEditEvent event) {
String lpcId=(String) ((LPC) event.getObject()).getLPCID();
System.out.println("EMAIL ID:"+lpcId);
}
public void onCancel(RowEditEvent event) {
System.out.println("inside on cancel");
}
}
答案 0 :(得分:1)
哎呀!!!我错过了datatable的选择属性。现在工作正常。
<p:dataTable var="lpcData" value="#{lpcBean.lpcIdList}" selection="#{lpcBean.selectedRows}"
id="lpcList" editable="true" scrollable="true" scrollWidth="1110"
tableStyle="table-layout:auto; width:1130px;" scrollHeight="330"
styleClass="datatable">