Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 2 将选中复选框的值从primefaces数据表中提取到托管bean_Jsf 2_Primefaces_Datatable - Fatal编程技术网

Jsf 2 将选中复选框的值从primefaces数据表中提取到托管bean

Jsf 2 将选中复选框的值从primefaces数据表中提取到托管bean,jsf-2,primefaces,datatable,Jsf 2,Primefaces,Datatable,我正在尝试从primefaces datatable中提取复选框SelectedValues selectedCars到名为TableBean的托管bean。我在尝试提取函数getSelection中的值时出现空指针异常请帮助我 我的JSF页面: <h:form id="form"> <p:dataTable id="multiCars" var="car" value="#{tableBean.mediumCarsModel}" paginator="true"

我正在尝试从primefaces datatable中提取复选框SelectedValues 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>
我的豆子

@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;
  }
}
如果列表为空,这将引发NullPointerException

如果你真的想使用它,那么你应该检查列表是否为空

if(selectedCars!=null && !selectedCars.isEmpty()){
    System.out.println(selectedCars[0].getModel()); 

}

不要在构造函数中调用getSelection。所选车辆将始终为空

谢谢您的建议。我想我不清楚我的问题,当我在jsf页面中按下view按钮时,我只想在我的bean类中的复选框中获取所选的值。但是,我能够在jsf页面的对话框中显示所选的值。您给出的建议只是防止获取空指针异常。
if(selectedCars!=null && !selectedCars.isEmpty()){
    System.out.println(selectedCars[0].getModel()); 

}