1

SchoolYear model

 public class SchoolYear {
    int id;
    int start;
    int end;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getEnd() {
        return end;
    }

    public void setEnd(int end) {
        this.end = end;
    }
}

SchoolYearDaoImpl class method

public List<SchoolYear> getAllSchoolYearStart() {
        String SQL = "{CALL getAllSchoolYearInfo()}";
        SchoolYear schoolyear = new SchoolYear();
        List<SchoolYear> list = new ArrayList<>();
        try(Connection con = DBUtil.getConnection(DBType.MYSQL);
                CallableStatement cs = con.prepareCall(SQL);){
            try(ResultSet rs = cs.executeQuery();){
                while(rs.next()){
                    schoolyear.setStart(rs.getInt("yearFrom"));
                    list.add(schoolyear);
                }

                JOptionPane.showMessageDialog(null,list.size());
                for(int x=0; x<list.size(); x++){
                    JOptionPane.showMessageDialog(null,((SchoolYear)list.get(x)).getStart());
                }
            }
        }catch(SQLException e){
            JOptionPane.showMessageDialog(null,e.getMessage());
        }
        return list;
    }

The result set I get by calling my stored procedure getAllSchoolYearInfo() shows 2 rows for yearFrom column.

enter image description here

However, when I ran the program to see what's on my GUI, I get 2 values in my JComboBox which appears to be the row 2's value 2015

enter image description here

As you can see I tried to get the size of the list to see how many objects are stored. So I'm guessing it could be a problem with how objects are stored and accumulated to the List<SchoolYear> in this block of code.

try(ResultSet rs = cs.executeQuery();){
                while(rs.next()){
                    schoolyear.setStart(rs.getInt("yearFrom"));
                    list.add(schoolyear);
                }

                JOptionPane.showMessageDialog(null,list.size());
                for(int x=0; x<list.size(); x++){
                    JOptionPane.showMessageDialog(null,((SchoolYear)list.get(x)).getStart());
                }
            }

Or could the problem be with my ListCellRenderer? I don't think it is.

jcmbSchoolYearStart.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if(value instanceof SchoolYear){
                    SchoolYear schoolyear = (SchoolYear) value;
                    setText(""+schoolyear.getStart());
                }
                return this;
            }
        } );

Any thoughts?

Thanks.

heisenberg
  • 1,784
  • 4
  • 33
  • 62
  • 2
    You're using a single instance of `SchoolYear` in your `getAllSchoolYearStart` method, which is been update by each result from the database, which is then been added to the `List` repeated, but it's still the same instance. Instead, create a new instance of `SchoolYear` in the `while(rs.next()){` loop – MadProgrammer Feb 03 '17 at 03:43
  • Possible duplicate of [Why does my ArrayList contain N copies of the last item added to the list?](http://stackoverflow.com/questions/19843506/why-does-my-arraylist-contain-n-copies-of-the-last-item-added-to-the-list) – 4castle Feb 03 '17 at 03:47

1 Answers1

3

You are only creating one SchoolYear instance. Because you do that, you only have a single reference in your list. You could comment out your current SchoolYear schoolyear and move it into the loop (and fix everywhere else you're using it), or just declare a new local variable in the loop like.

while(rs.next()){
    SchoolYear sy = new SchoolYear();
    sy.setStart(rs.getInt("yearFrom"));
    list.add(sy);
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249