1

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();
        }
2
  • can you update your question with the error stacktrace? Commented Oct 29, 2013 at 13:55
  • java.sql.SQLException: After end of result set Commented Oct 29, 2013 at 14:00

4 Answers 4

3

Try with the test :

x < 3 && lenghtCounter>0
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! :) This works perfect. Now I see the mistake. I thought it is the condition to terminate the operation. But it is the condition to continue the operation. Is that right ? A small explaination would be helpful :)
You're right : the condition in an if statement should be red as a condition in a while. It continues until the condition becomes false.
2

|| won't work as expected here because the for loop condition works the other way. A for loop condition is the same as a while loop condition. The loop breaks when the condition you specify evaluates as false. So the way you've specified it, the loop will run while either x < 3 || lenghtCounter == 0.

I think what you want is:

x < 3 && lenghtCounter != 0

or:

x < 3 && lenghtCounter > 0.

(Also lenghtCounter a typo?)

Comments

1

You probably don't need || lenghtCounter==0. When lengthCounter reaches 0 you call rs.next() one extra time, and that's why you're possibly getting the exception.

Comments

1

If I had to guess you are trying to not have the inner loop run until the outer loop has run three times. However, each time the outer loop runs, x is reset to 0 due to scope. Recheck your scoping - declare x before the loops.

Additionally, your outer loop will do nothing by itself. You may want to switch to a while loop with multiple logic incrementers for x and y.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.