我正在尝试从 primefaces 数据表中将复选框选定值selectedCars 提取到名为TableBean的托管 bean中。我在尝试获取函数getSelection()中的值时遇到空指针异常, 请帮助我解决这个问题
我的 JSF 页面:
<h:form id="form">
<p:dataTable id="multiCars" var="car"
value="#{tableBean.mediumCarsModel}" paginator="true" rows="10"
selection="#{tableBean.selectedCars}">
<f:facet name="header">
Checkbox Based Selection
</f:facet>
<p:column selectionMode="multiple" style="width:2%" />
<p:column headerText="Model" style="width:25%">
#{car.model}
</p:column>
<p:column headerText="Year" style="width:25%">
#{car.year}
</p:column>
<p:column headerText="Manufacturer" style="width:24%">
#{car.manufacturer}
</p:column>
<p:column headerText="Color" style="width:24%">
#{car.color}
</p:column>
<f:facet name="footer">
<p:commandButton id="multiViewButton" value="View"
icon="ui-icon-search" update=":form:displayMulti"
oncomplete="multiCarDialog.show()" />
</f:facet>
</p:dataTable>
<p:dialog id="multiDialog" header="Car Detail"
widgetVar="multiCarDialog" height="300" showEffect="fade"
hideEffect="explode">
<p:dataList id="displayMulti" value="#{tableBean.selectedCars}"
var="selectedCar">
Model: #{selectedCar.model}, Year: #{selectedCar.year}
</p:dataList>
</p:dialog>
</h:form>
我的托管 bean
@SessionScoped
@ManagedBean
public class TableBean implements Serializable {
private List<Car> cars;
private Car[] selectedCars;
private CarDataModel mediumCarsModel;
Connection connection;
Statement stmt;
ResultSet rs;
public TableBean() {
cars = new ArrayList<Car>();
getCars();
getSelection();
mediumCarsModel = new CarDataModel(cars);
}
public Car[] getSelectedCars() {
return selectedCars;
}
public void setSelectedCars(Car[] selectedCars) {
this.selectedCars = selectedCars;
}
public void getCars() {
int i = 0;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:jtds:sqlserver://cvgapp106I/dev2_LPSR");
System.out.println("connected to the database");
stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("select * from test");
while(rs.next()) {
cars.add(i,new Car(rs.getString("Model"),rs.getInt("Year"),rs.getString("Manufacturer"),rs.getString("Color")));
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void getSelection() {
System.out.println(selectedCars[0].getModel());
}
public CarDataModel getMediumCarsModel() {
return mediumCarsModel;
}
public void setMediumCarsModel(CarDataModel mediumCarsModel) {
this.mediumCarsModel = mediumCarsModel;
}
}