Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7091641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:12:41+00:00 2026-05-28T08:12:41+00:00

Unfortunately I’ve got a bug that I have been working on for 1 full

  • 0

Unfortunately I’ve got a bug that I have been working on for 1 full day now, and though I know there are ways to work around it, I am very excited to learn where my logic goes wrong.

Now, my program is about a match between two teams of chessplayers. It predicts the scores depending on the player-setup chosen by the coach (the user). *(more background info below my question)

So I first ask the user which players are in his team. Ofcourse I want this “PlayerList” information to stay constant (and the bug I referred to is that it mysteriously doesnt)

So I made a Gui that pops up all the boards, with the players in the order that the user submitted them to the program. Then whenever he clicks one of the buttons, the acionlistener of that button should display the next player, (and it lets the main program know that there was another player selected on that board and adjust the prediction accordingly, but these tasks work perfectly).

Now, the problem is that if a user has replaced for example Marcel on board 2 for someone else, that when the user is going to click on the button for board 3, he no longer has the options Marcel at his disposal. The “playerList”-field of all buttons has been set to the chosen player setup in the main program, instead of the original list that the main program prompted the user for at the start of the run…

(see attached image)enter image description here

So there is some really annoying dependency between the players that are assigned to these buttons, and the actually chosen player list that has been made by the user. If I look at my code I cannot comprehend why…

So something goes wrong with them buttons that I defined.

now this is what we’ve got:

I extended JButton, so that the button has the info on the available players in the users team like this:

    public class JButtonPlus extends JButton {
public Team team;
private int aantalSpelers;
final ArrayList<Player> playerList;
private Integer spelerNr;
private Integer bordNr;


public JButtonPlus(ArrayList<Player>playerList_, Integer spelernr, Integer bordnr){

    playerList=playerList_;
    aantalSpelers=playerList_.size();
    spelerNr=spelernr;
    bordNr = bordnr;
}

So the problem is that this final ArrayList playerList is not constant at all.

It changes after invoking any of the actionListeners by clicking any of the other buttons.

This is the code in the main program that makes the buttons and adds the actionlisteners:
(note: this teams.get(0) refers to the user’s team, and the field players refers to an ArrayList that got filled with all of the players that he submitted to the program. The method displaySpelernr(i) adds the playername to the setText method of the JButtonPlus)

Box verticalBox_1 = Box.createVerticalBox();
    frame.getContentPane().add(verticalBox_1);

    JLabel lblNewLabel = new JLabel("opstelling thuisteam");
    verticalBox_1.add(lblNewLabel);

    for (int i = 0; i < teams.get(0).players.size(); i++) {
        final JButtonPlus btnSpeler = new JButtonPlus(
                teams.get(0).players,i, i);

        btnSpeler.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                btnSpeler.displayVolgendeSpeler();

            }
        });
        verticalBox_1.add(btnSpeler);
        btnSpeler.displaySpelerNr(i);
    }

the method that is invoked at userClick, displayVolgendeSpeler is like this

    public void displayVolgendeSpeler(){
spelerNr =(spelerNr+1)%aantalSpelers;
    displaySpelerNr(spelerNr);      

the problem is above statement, other statements concern the calculation, which works like it should. This displaySpelerNr() method looks like his


public void displaySpelerNr(Integer i){
setText(playerList.get(i).SpelerNaam);

and here the bug gets noted: the playerList object has been changed..

Does anybody see why this supposedly constant source of players playerList changes when a button is clicked?

*(more background info)
A match between two chessteams involves all players to play 1 match against one of the opponents players. Typically, the players in a team are of different strength. So for example, if youve got team A with player strength ranging from beginner to master, and team B with players ranging from intermediate to grandmaster, you know that if you pair the players based on increasing strength, then team A will lose for sure. The fun starts when Team A will sacrifice one or two of their weakest players by pairing them to the two strongest players of Team B. This means a loss of two points, but the remaining players of team A will now each face a weaker player instead of a stronger one. There might even be pairings so that it is more likely that the weaker team wins. My program tries to show which pairings are worst and best, and gives the statistics. Ofcourse it would be nice if the coach that might use this program is actually able to get this Gui to be set at his desired team, and this is where you come in, dear StackOverflow-champ!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:12:41+00:00Added an answer on May 28, 2026 at 8:12 am

    Declaring a field as final means that you can’t assign a new value to it. If, however, that field contains an Object, you can still modify non-final fields inside of that Object. The code below should illustrate what I mean.

    class Foo {
        private final Bar foo; 
    
        public Foo() {
            foo = new Bar();
        }
    
        public void testFoo() {
            this.foo = new Bar(); // compile error
            this.foo.setBar("Test"); // works fine
        }
    }
    
    class Bar {
        private String bar;
    
        public void setBar(String bar) {
            this.bar = bar;
        }
    
        public String getBar() {
            return bar;
        }
    }
    

    In your code, you have a final ArrayList<Player>, which means you can’t store a new ArrayList in that field. You can, however, modify the contents of the existing ArrayList in any of the usual ways (adding, removing, updating elements, etc).

    You’re also passing the same ArrayList to all of your JButtonPlus instances, so changing it in any of those classes will result in a change to all of them. Try passing in a copy of the ArrayList, and see if that solves the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Unfortunately it looks like for various reasons I'm going to have to use Visual
Unfortunately, there seems to be no string.Split(string separator), only string.Split(char speparator). I want to
Unfortunately I don't have access to a *nix box at work or at home.
Unfortunately, the problem is not more specific than that. I've found a few examples
Unfortunately I have to use Windows Server 2003 on my 32 bit workstation due
Unfortunately, for some reason I cannot fathom, I haven't been able to get windbg
Unfortunately the solutions have not worked yet; setting result.values pointer to 0 doesn't actually
I've got a string that has curly quotes in it. I'd like to replace
unfortunately I was never good with math but I would like to know how
unfortunately I have not found anything yet, maybe anyone could help me or give

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.