Causés par: java.lang.ArrayIndexOutOfBoundsException: Tableau d'index out of range: 0

J'ai un formulaire de mon entité (weatherstation) .
J'ai aussi un champ appelé "bezeichnung" (description). Quand j'entre dans une chaîne et appuyez sur le bouton de recherche (pour la recherche), je reçois toujours une Exception:

Causés par: java.lang.ArrayIndexOutOfBoundsException: index de Tableau hors
de gamme: 0

Dans ma base de données est un objet avec la bezeichnung "Traunstein"

Entité (avec getter et 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;
    }

Façade 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;
    }

Vue:

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

Contrôleur:

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

Dans le debugg mode, je vois dans la searchbez() que le "bezeichnung" est null.
J'ai utiliser le immediate="true" que les autres éléments ne sera pas analysé
S'Il Vous Plaît Aider

  • si(e.get(0)!=null) si vous n'avez pas la moindre résultat vous cochez la première elemnt (et vous n'avez pas le premier élément) c'est pourquoi vous avez indexoutofboundsException, et votre requête nommée mabey est faux: essayer .setParameter("bez", "%" +nom + "%")
InformationsquelleAutor user547995 | 2012-02-29