Cyanide
Cyanide

Reputation: 21

Trouble with created object added to a list

i've going through a little program and got an error that i'm not able to find solution for.

My objective was to add to a list ( used ArrayList ) a created object ( BlockObject extending from JPanel ) create by triggering a click, and then be able to read the list to print it on the screen, but at the time of reading such list i get ArrayOutOfBoundsException.

I've been debugging and found the reason of the trouble, and it's that i don't know why, each object that i create and add to it retrieve the latest type ( explained ahead ) and not the one with which it was created.

The code that runs along the list is

    public void ListBlock(){

    BlockObject auxb;
    Point auxp;

    for(int i = 0; i < list.size(); i++){
        auxb = list.get( i );

        if( auxb.getType() != 0 ){

            auxp = list.get( i ).limitBounds( 0, list.get( i - 1 ).getHeight() );
            auxb.setBounds( ( int ) auxp.getX(), ( int ) auxp.getY(), GlobalDataStorage.StatementWidth, GlobalDataStorage.StatementHeight );
        }
        add( auxb );
    }

}

The trouble said it's because it gets always in the if() condition, so even the first element takes the previous one ( being the cause of the OutOfBounds ), but there is already an element inside at each moment which would be type 0 to jump it, and the rest enter in it:

   list = new ArrayList<BlockObject>();
    startingBlock = new BlockObject( 0 );

The type there is 0, but it changes as explained ahead, that's what shouldn't happen and don't know why it does.

The following elements are added:

    public void mouseClicked( MouseEvent evt ) {

        point = new Point( evt.getX(), evt.getY() );;

        if( ( findComponentAt( point ) instanceof BlockObject ) && ( StatementPanel.getType() != 0 ) ){

            list.add( new BlockObject( StatementPanel.getType() ) );

            ListBlock();
            revalidate();
            repaint();

        }

    }

The type at the BlockObject creation it's saved in a variable, and it's supposed to not change, so each creation i thought that would be saved in the list with a value, but it keeps changing all the previous objects created when at StatementPanel a new leaf it's selected ( StatementPanel it's implemented following a JTree, each leaf a different type ). Though maybe that was the static method to get the type, but didn't work adding a different one.

Maybe the problem is obvious and i can't see it.

Upvotes: 0

Views: 71

Answers (1)

Goran Štuc
Goran Štuc

Reputation: 581

i assume you have this problem:

Example e = new Example();
e.ChangeValue("e");
List<Example> examples = new List<Example>();
examples.add(e);
e.ChangeValue("e2");
examples.add(e);

the problem with this is that if you add e to the list it just creates a reference to the object and both added to the list are pointing to the same reference (so changing one would change the other too), solution to this would be Create a new Object a new Example and copy it's values to it, that way you would not have the reference problem and could change only one at a time

Upvotes: 1

Related Questions