引起:java.lang.ArrayIndexOutOfBoundsException:数组索引超出范围:0

use*_*995 0 java jsf jpa

我有一个来自我的实体的形式(weatherstation).我还有一个名为"bezeichnung"的字段(描述).当我输入一个字符串并按下搜索按钮(用于搜索)时,我总是得到一个异常:

引起:java.lang.ArrayIndexOutOfBoundsException:数组索引超出范围:0

在我的数据库中是bezeichnung"Traunstein"的一个对象

实体(带有getter和setter):

@Entity
@NamedQuery(name="findbybez", query="select w from Weterstation w where w.bezeichnung like :bez")
public class Weterstation implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(nullable=false, unique=true)
    @Length(min=3, max=20, message="Min.->3 Max->20")
    private String bezeichnung;

    @Column(nullable=false)
    @Pattern(regexp="[0-9][0-9][0-9][0-9]" , message="PLZ ist falsch")
    private String plz;

    @Column(nullable=false)
    @Length(min=3, max=20, message="Min.->3 Max->20")
    @Pattern(regexp="^[A-Z][a-z]*", message="Der Ort muss einen Anfangsbuchstaben machen")
    private String ort;

    @Column(nullable=false)
    @TempValid(message="Min -40°C, Max 40°C")
    private double temp;


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
Run Code Online (Sandbox Code Playgroud)

Facade Methode:

@Override
    public Weterstation findbyBezeichnung(String name) {
      List e =  em.createNamedQuery("findbybez").setParameter("bez", name).getResultList();
      Weterstation w = new Weterstation();
      if(e.get(0)!=null)
      {
          w = (Weterstation)e.get(0);
      }
        return w;
    }
Run Code Online (Sandbox Code Playgroud)

视图:

<f:view>
                <h:form>
                    <h1><h:outputText value="Create/Edit"/></h1>
                    <h:panelGrid columns="3">
                        <h:outputLabel value="Id:" for="id" />
                        <h:inputText id="id" value="#{wettercontroller.station.id}" title="Id" disabled="true"/>
                        <h:message for="id"></h:message>
                        <h:outputLabel value="Bezeichnung:" for="bezeichnung" />
                        <h:inputText id="bezeichnung" value="#{wettercontroller.station.bezeichnung}" title="Bezeichnung" required="true" requiredMessage="The Bezeichnung field is required."/>
                        <h:message for="bezeichnung"></h:message>
                        <h:outputLabel value="Ort:" for="ort" />
                        <h:inputText id="ort" value="#{wettercontroller.station.ort}" title="Ort" required="true" requiredMessage="The Ort field is required."/>
                        <h:message for="ort"></h:message>
                        <h:outputLabel value="Plz:" for="plz" />
                        <h:inputText id="plz" value="#{wettercontroller.station.plz}" title="Plz" required="true" requiredMessage="The Plz field is required."/>
                        <h:message for="plz"></h:message>
                        <h:outputLabel value="Temp:" for="temp" />
                        <h:inputText id="temp" value="#{wettercontroller.station.temp}" title="Temp" required="true" requiredMessage="The Temp field is required."/>
                        <h:message for="temp" ></h:message>

                    </h:panelGrid>
                    <h:commandButton value="Create" action="#{wettercontroller.createWetter()}"></h:commandButton>
                    <h:commandButton value="SearchBez" action="#{wettercontroller.searchbez()}" immediate="true" ></h:commandButton>
                </h:form>
            </f:view>
Run Code Online (Sandbox Code Playgroud)

控制器:

@ManagedBean
@SessionScoped
public class Wettercontroller implements Serializable {

    @EJB
    WeterstationFacadeLocal wetterfacade;

    private Weterstation station;

    private long id;





    /** Creates a new instance of Wettercontroller */
    public Wettercontroller() {
        station = new Weterstation();
    }
        public void searchbez()
    {
        station = wetterfacade.findbyBezeichnung(station.getBezeichnung());
    }
Run Code Online (Sandbox Code Playgroud)

在debugg-mode中,我看到searchbez()"bezeichnung"为null.我必须使用immediate="true"其他元素不会被解析请帮助

Bal*_*usC 6

该错误在您的Facade方法中.

List e =  em.createNamedQuery("findbybez").setParameter("bez", name).getResultList();
Weterstation w = new Weterstation();

if (e.get(0) != null) {               // <--- Here
    w = (Weterstation) e.get(0);
}

return w;
Run Code Online (Sandbox Code Playgroud)

如果列表为,则可能不可能是索引处的项目0.你需要以不同的方式检查它.

例如

if (!e.isEmpty()) {
    w = (Weterstation) e.get(0);
}
Run Code Online (Sandbox Code Playgroud)

要么

if (e.size() == 1) {
    w = (Weterstation) e.get(0);
}
Run Code Online (Sandbox Code Playgroud)

回到具体的功能要求,immediate="true"在命令按钮上设置将导致在应用请求值,验证和更新模型值阶段期间完全跳过没有设置此属性的所有其他输入字段.它们将null在调用动作阶段结束.由于您似乎确实需要bezeichnung输入字段的值来执行操作,因此您还需要添加immediate="true"到该输入字段.

<h:inputText id="bezeichnung" ... immediate="true" />
Run Code Online (Sandbox Code Playgroud)

或者,既然你已经在JSF 2.x上,我建议你去看一下<f:ajax>部分执行和渲染表单.例如

<h:commandButton value="SearchBez">
    <f:ajax execute="@this bezeichnung" listener="#{wettercontroller.searchbez}" render="@form" />
</h:commandButton>
Run Code Online (Sandbox Code Playgroud)