Note that there are some explanatory texts on larger screens.

plurals
  1. POArrayList.size() returning incorrect value
    primarykey
    data
    text
    <p>I have a strange error which I can not get my head around where the .size() method does not appear to return the correct value.</p> <p>This first bit of code creates the ArrayList.</p> <pre><code>public void createResultList(String query) { ArrayList&lt;ArrayList&lt;String&gt;&gt; data = new ArrayList(); try { ResultSet rs = st.executeQuery(query); ResultSetMetaData meta = rs.getMetaData(); for(int i = 0; i &lt; meta.getColumnCount(); i++) { data.add(i, new ArrayList&lt;String&gt;()); } int x = 0; while(rs.next()) { for(int y = 0; y &lt; meta.getColumnCount(); y++) { data.get(x).add(rs.getString(y + 1)); } x++; } } catch(Exception e) { JOptionPane.showMessageDialog(null, e); } ResultTable result = new ResultTable(data); JTable table = new JTable(result); JScrollPane scrollpane = new JScrollPane(table); add(scrollpane); refresh(); } </code></pre> <p>This is my TableModel class which is used to create the table when it's passed to it.</p> <pre><code>public class ResultTable extends AbstractTableModel { private ArrayList&lt;ArrayList&lt;String&gt;&gt; data; public ResultTable(ArrayList&lt;ArrayList&lt;String&gt;&gt; data) { this.data = data; } public int getColumnCount() { return data.get(0).size(); } public int getRowCount() { return data.size(); } public Object getValueAt(int row, int col) { return data.get(row).get(col); } } </code></pre> <p>Now the the problem is in the ResultTable class, now for a select query returning one row with 12 columns, the first data.get(0).size() call correctly returns 12, but the 2nd data.size() call incorrectly returns 12 also instead of 1, this is causing out of bounds errors, can anyone please explain this seemingly paradoxical result?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload