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

标签 java jsf jpa

我有来 self 的实体(气象站)的表格。 我还有一个名为“bezeichnung”(描述)的字段。当我输入一个字符串并按下搜索按钮(用于搜索)时,我总是得到一个异常:

Caused by: java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0

在我的数据库中有一个带有“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;
    }

外观方法:

@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;
    }

查看:

<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>

Controller :

@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());
    }

在 Debug模式下,我在 searchbez() 中看到“bezeichnung”为空。 我必须使用 immediate="true" ,其他元素将不会被解析 请帮忙

最佳答案

该错误存在于您的外观方法中。

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;

如果列表为空,那么索引 0 处可能不可能有一个项目。您需要以不同的方式检查它。

例如

if (!e.isEmpty()) {
    w = (Weterstation) e.get(0);
}

if (e.size() == 1) {
    w = (Weterstation) e.get(0);
}

回到具体的功能需求,设置immediate="true"命令按钮上的 将会导致所有其他未设置此属性的输入字段在应用请求值、验证和更新模型值阶段完全跳过。它们最终将成为 null在调用操作阶段。因为您似乎实际上需要 bezeichnung 的值输入字段来执行操作,您需要添加 immediate="true"也到该输入字段。

<h:inputText id="bezeichnung" ... immediate="true" />

或者,由于您已经在使用 JSF 2.x,我建议您查看 <f:ajax>部分执行并呈现表单。例如

<h:commandButton value="SearchBez">
    <f:ajax execute="@this bezeichnung" listener="#{wettercontroller.searchbez}" render="@form" />
</h:commandButton>

关于java - 由 : java. lang.ArrayIndexOutOfBoundsException 引起:数组索引超出范围:0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9503273/

相关文章:

java - 如何让多对多关系船的双方拥有?

java - 内部带有 JTextArea 组件的自定义 JButton

java - 如何使用Java读取原始串行数据?

Java多态方法无法解析

java - 有哪些免费工具可用于分析 java 中的锁争用?

java - 在 JSF 中获取请求参数值

jsf - JSF中限制文件上传大小

JSF 1.2 Tomcat 重新部署错误

当 LocalDate 为 null 时,Java 8 LocalDate JPA 2.2 坚持到 postgresql Date 失败

java.lang.IllegalArgumentException : Unknown entity: com. myapplication.entities.PersonClass