brendan
brendan

Reputation: 3482

ClassCastException: uirepeat index in setting the boolean array value jsf

The array size is depend on the student size on runtinme. I am able to display the boolean effectively on the selectBooleanCheckbox in JSF based on the array boolean. However, in setting the value, the ClassCastException occur.

java.lang.ClassCastException at javax.el.ArrayELResolver.setValue(ArrayELResolver.java:260)

In Managed Bean:

enrollarr = new boolean[this.student.size()];

 public boolean[] getEnrollarr() {
    return enrollarr;
}

public void setEnrollarr(boolean[] enrollarr) {
    this.enrollarr = enrollarr;
}

In JSF:

    <ui:repeat var="value" value="#{adminController.student}" varStatus="myvar" >
                <tr>
                    <td>#{value.name}</td>  
                    <td>#{value.TP}</td> 
                    <td>#{value.gender}</td> 
                    <td><h:selectBooleanCheckbox value="#{adminController.enrollarr[myvar.index]}" /></td>
                </tr>
    </ui:repeat>

Upvotes: 0

Views: 115

Answers (1)

Ingo K.
Ingo K.

Reputation: 79

You got some different Problems in your question.

First of all: The size of an array cant never be set at runtime. An array has to allocate memory before runtime. This means your:

 enrollarr = new boolean[this.student.size()];

will never work. You try to allocate memory during your runtime.

For this case i suggest to use an ArrayList an fill it with booleans like that.

 List<boolean> enrollList = new ArrayList<>;

..and add vaules with his funtion.

enrollList.add(new boolean(true));

This maybe won't avoid the classCast case.

So second: I guess your class cast is thrown here:

<td><h:selectBooleanCheckbox value="#{adminController.enrollarr[myvar.index]}" /></td>

You try to call adminController.(get/set)enrollarr[myvar.index]... But this function doesnt exists.

"Translated" the frontEnd tries to call adminController.setEnrollarr(true) and as we can see: there is no function like .setEnrollarr(boolean) only .setEnrollarr(boolean[])].

Third: But in general i think you construction is a little bit confused. It would be much easier to move you enroll- boolean to the Student class. Then you can call it like the other varibles:

 <ui:repeat var="value" value="#{adminController.student}" varStatus="myvar" >
            <tr>
                <td>#{value.name}</td>  
                <td>#{value.TP}</td> 
                <td>#{value.gender}</td> 
                <td><h:selectBooleanCheckbox value="#{value.enroll}" /></td>
            </tr>
</ui:repeat>

And here the addtion to your Stundent class. Be aware: Boolean getter start with "is".

 public class Student{

    public boolean enroll;

    public boolean isEnroll() {
        return enroll;
    }

    public void setEnroll(boolean enroll) {
        this.enroll = enroll;
    }

 }

Hope my answear could bring some clearification and helped you to go further!

Upvotes: 1

Related Questions