-1

I am getting the error:

javax.faces.FacesException: Value of 'frmrapport:type' must be an array or a collection

from the XHTML file:

<p:selectManyMenu id="type" required="true"
        value="#{userReporting.getTypeParId(userReporting.selected)[0].nomType}">
    <f:selectItem itemLabel="co" itemValue="co" />
    <f:selectItem itemLabel="pi" itemValue="pi" />
    <f:selectItem itemLabel="si" itemValue="si" />
</p:selectManyMenu>

from the Java bean:

public List getTypeParId(int id){
    return this.genTypeFacade.getTypeParId(id);
} 

the problem is that the bean is a List and I can't convert the list to a String[].

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    If the question is "How do I convert List to String[]", it's a duplicate **and** extremely easy to find on internet and SO. But I believe that's not your real problem. – Turtle Jun 15 '17 at 14:43

1 Answers1

-1

userReporting.getTypeParId(userReporting.selected) is returning a List. You can't access List[0], you must use List#get(int index).

value="#{userReporting.getTypeParId(userReporting.selected).get(0).nomType}"

Also, be carefult with generics: it's better if you specify what contains the collection you return, i.e.:

public List<Type> getTypeParId(int id){
     return this.genTypeFacade.getTypeParId(id);
}

Another "also": Your method getTypeParId returns more than 1 type? If yes, it should be called getTypesParId.

Turtle
  • 1,626
  • 16
  • 26
  • Your answer is correcterer than mine, I better remove my answer. – hamena314 Jun 15 '17 at 14:39
  • @hamena314 You could've let it, as it answered the question of TO; not the problem, but the question :p – Turtle Jun 15 '17 at 14:41
  • javax.faces.FacesException: Value of 'frmrapport:type' must be an array or a collection – Mohamed Nani Jun 15 '17 at 14:51
  • @MohamedNani That's an exception, indeed. Maybe you wanted to wrap it in a sentence? :p If you have troubles in english, you can explain it in french in the comments (of this answer) and I'll update your question. – Turtle Jun 15 '17 at 14:53
  • selectManyMenu accepte String[] or collection only but my method is list i want solution to convert result of list to String[] – Mohamed Nani Jun 15 '17 at 14:56
  • @Nathan: I dont want to answer the question of the TO, I want to help him. This is a big difference if it means that I need to answer questions of the TO he has not even asked. And in that case your answer is probably much closer to what TO needs help with. :) – hamena314 Jun 15 '17 at 15:06
  • @MohamedNani First of all, if it accepts a `Collection`, your list is ok: the problem is that you're trying to take it's first element ([0]). Then, if you really want to convert your list to an array, you can look at [this post](https://stackoverflow.com/questions/4042434/converting-arrayliststring-to-string-in-java) that explains it. – Turtle Jun 15 '17 at 15:08
  • 1
    @hamena314 Well it seems he really wants a List, so I'm gonna flag it back as a duplicate :p – Turtle Jun 15 '17 at 15:08
  • @Nathan i try get(0) but the same problem , i want convert result of list to string[] – Mohamed Nani Jun 15 '17 at 15:13
  • @Nathan i try this ` public String[] getTypeParId(int id){ String[] stringArray; stringArray = (String[]) this.genTypeFacade.getTypeParId(id).toArray(new String[0]); return stringArray; } ` the error Grave: java.lang.ArrayStoreException – Mohamed Nani Jun 15 '17 at 15:22