0

Can someone please tell me how to get the values of a particular index in my ArrayList to display? When I run the program the values do not display. I figured it out. Thanks! had to include the @PostConstruct

I am using Eclipse

// This is the JAVA file
package jsf2demo;

import java.util.*;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.inject.Named;
import java.io.Serializable;
import java.util.ArrayList;


@ManagedBean(name = "quiz")
@SessionScoped
public class QuizJSFBean implements Serializable {

    private ArrayList<Integer> list = new ArrayList<Integer>();

    public QuizJSFBean() {

    }

    public void setList() {
        for (int i = 0; i < 10; i++) {
            list.add((int) (Math.random() * 100));
        }
    }

    public List<Integer> getList() {
        return this.list;
    }
}
// This is the separate HTML file
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Quiz</title>
    </h:head>
    <h:body>
        <h:form>
            <h:panelGrid columns="6">
                <h:outputLabel  style="text-align: right"
                                value="#{quiz.list[0]}" />

                <h:outputLabel value="+" />
                <h:outputLabel  style="text-align: right"
                                value="#{quiz.list[1]}" />
                <h:outputLabel value="=" />
             </h:panelGrid>
        </h:form>
    </h:body>
</html>

// Not sure what addition detail I need to add

F A
  • 1
  • 2
  • `quiz.list[0]` is the same as writing `quiz.list.get(0)` in EL. This should work flawlessly. The actual problem may be disguised somewhere else. – Tiny Nov 09 '15 at 03:33
  • Besides that, use a method decorated with `@PostConstruct` to create and initialize the list (Further, use the abstraction `List` rather than its concrete implementation `ArrayList`). – Tiny Nov 09 '15 at 03:45
  • 1
    When do you initialize your list ? We see that you are doing it in the `setList`, but not sure when you call it ! – Mooolo Nov 09 '15 at 03:51
  • Hi yes I added a main method in the java file and created a new QuizJSFBean and implemented setList(), but still no luck. Any other ideas? – F A Nov 09 '15 at 23:37
  • Just click the duplicate question link for the answer. – BalusC Nov 10 '15 at 06:37
  • Thank you! That did address the issue I was having. Much appreciation – F A Nov 10 '15 at 13:10

0 Answers0