Java Iterator | Increment within a sentinel

Asked

Viewed 32 times

1

public void actionPerformed(ActionEvent e) {
            i++;
            ImageIcon icon = new ImageIcon(files[i].getAbsolutePath());
            imagem.setIcon(icon);

            frame.add(imagem, BorderLayout.CENTER);  
}
});

I have an array of images, and the goal is always to press the button (event), the i iterator increments its value in a unit and mutes image.

But when I get to the end of the array (when there are no images), it gives me error and blocks the program. I know I have to do an if(files.lenght) but I’m not sure where to put it. Can someone help me? Thanks

  • What do you want to do? Count how many times the user has clicked the button?

  • 1

    Where’s the increment in the code? You wouldn’t happen to be popping the array size? Add a [mcve] by editing the question.

  • am popping yes, I edited the post. The if condition is that it’s messing me up

  • 1

    Add the stack of errors as well.

  • @jbueno when reaching the end of the array size I want not to error, and to launch an exception for example

1 answer

2


Just check if the Internet exists before trying to access it.

public void actionPerformed(ActionEvent e) {
    i++;

    if(i < files.length()){
        ImageIcon icon = new ImageIcon(files[i].getAbsolutePath());
        imagem.setIcon(icon);    
        frame.add(imagem, BorderLayout.CENTER);  
    } else {
        //fazer alguma coisa
    }
}});

Note: You can not understand very well what you want to do. This will work, but maybe it shouldn’t be done like this, if you give more details I can improve the answer.

  • Honestly, that’s right, I just found out that my Workspace was being rewritten so that’s why I was making a mistake. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.