Looping in ArrayList isn't working (Get() method)

I'm looking for days something to help me, but I didn't find anything for this problem and I really don't know what to do.

Here is the problem... I'm trying to get the url I added in this ArrayList (urlPage) in the position i inside my loop, but I got this compilation error: Local variable i defined in an enclosing scope must be final or effectively final. I've tried many things, but nothing works.

/* Create a loop starting with 0 and ending with 3 to add all the components into the panel */
        for (int i = 0; i < 3 ; i++) {
            productIconLabel[i] = new JLabel("");
            productIconLabel[i].addMouseListener( new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent arg0) {
                    try {
                        Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + urlPage.get(i));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }  
                }
            });

The code in the loop continues (there's why here there's no end bracket to for), but this is the part that I'm having problems with. This is the function where I add the url into the ArrayList.

/* Function to receive part of information sent from the form page */
    public void radioButtonResult(int productIndexNumber, String productImageAddress, String productName, double productPrice, String url) {
        productIconLabel[productIndexNumber].setIcon(new ImageIcon(productImageAddress));
        productNameLabel[productIndexNumber].setText(productName);
        productPriceLabel[productIndexNumber].setText(Double.toString(productPrice) + " €");
        urlPage.add(productIndexNumber, url);
    }

The only problem is the compilation error. To make some tests I've changed i for 0, 1 and 2 and its worked. I appreciate any help.

Upvotes: 2

Views: 85

Answers (3)

Titus
Titus

Reputation: 22474

i needs to be final in order to access it inside of a method of an anonymous inner class. You can do something like this:

for (int i = 0; i < 3 ; i++) {
        final int fi = i;
        productIconLabel[i] = new JLabel("");
        productIconLabel[i].addMouseListener( new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent arg0) {
                try {
                    Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + urlPage.get(fi));
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });
      .....

Upvotes: 2

Rahman
Rahman

Reputation: 3785

You cant use local variable i in an anonymous inner class. Because those 2 scopes are different.

Upvotes: 1

Mateo Barahona
Mateo Barahona

Reputation: 1391

You're in new MouseAdapter(), so it doesn't know the variable i. Different scope.

Use a final variable.

for (int i = 0; i < 3 ; i++) {
            final int currentIter = i;
            productIconLabel[i] = new JLabel("");
            productIconLabel[i].addMouseListener( new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent arg0) {
                    try {
                        Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + urlPage.get(currentIter));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            });

Upvotes: 2

Related Questions