I have coded two for-loops which should fill a GridBagLayout. Somehow the second condition does not come to operation and therefore I get an exception.
So basically what the program does: It gets the data from a MySQL Server and lists it in a GridBagLayout. Therefore I get the lenght of the row by using rs.lenght and rs.getRow(). Than I copy it to lenghtCounter for the inner for-loop(with the x variable). The inner for-loop should be executed after 3 cycles OR if lenghtCounter is equal to 0. I see in the console that the counter works and gets down to 0 as expected but the for-loop does not break. Instead it continues and gives me an exception.
Here is the code:
try {
ResultSet rs = MySQL.getStatement().executeQuery(
"select * from obj_house");
rs.last();
int lenghtCounter = rs.getRow();
int lenght = lenghtCounter;
rs.first();
System.out.println(lenghtCounter + " " + (lenght / 3));
for (int y = 0; y <= (lenght / 3); y++) {
for (int x = 0; x < 3 || lenghtCounter==0; x++) {
System.out.println("ID " + lenghtCounter);
JButton street = new JButton("Strasse: " + rs.getString(5));
gbcOut.gridx = x;
gbcOut.gridy = y;
panelBottom.add(street, gbcOut);
System.out.println("X: " + x);
System.out.println("Y: " + y);
rs.next();
lenghtCounter--;
System.out.println("lenght: " + lenghtCounter);
}
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}