Question

i edit the my code to slove the last prob but now when i'm doing System.out.println(uns.get(0)); i get NULL but System.out.println(uns.get(0)); it i ger result !!!! oopps

   Ok I SOLVE THE PROB NOW  i'm adding  uns=new ArrayList(10);
    for (int ii = 0; ii < 10; ii++) {
    uns.add(null);
    }     to Construct

   http://i.stack.imgur.com/9rNTa.png

and i edit these instruction

if(i==0)
{ uns.add(i,u);

}
if(i==1)
{
uns.add(i,u);

}   
i++;

Thank for all

private void jTable1MouseClicked(java.awt.event.MouseEvent evt)   {                                     
int ligneNom =   jTable1.getSelectedRow();
int colonneNom = jTable1.getSelectedColumn();
DefaultTableModel model = (DefaultTableModel) jTable2.getModel();

     row = new Vector();
    if(colonneNom==0)
    {   
        try {

            int numUnite=Integer.parseInt(jTable1 .getValueAt(ligneNom, 0).toString());
             operation3 o3=new operation3();
             uns=new ArrayList(10);
             for (int ii = 0; ii < 10; ii++) {
              uns.add(null);
             }

             System.out.println(numUnite);
             u=o3.getUnite(numUnite);
             System.out.print(u);
             uns.add(i,u);
             System.out.println(uns.get(0));
             System.out.println(uns.get(1));


            i++;

            row.add( jTable1 .getValueAt(ligneNom, 0));
            row.add( jTable1 .getValueAt(ligneNom, 1));
            model.addRow(row);
            jTable1.clearSelection();

       } catch (NamingException ex) {
           Logger.getLogger(employeihm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}                                    
Was it helpful?

Solution

Your misunderstanding is here:

            uns=new ArrayList(10);

This creates a list whose capacity is 10 ... but that initially contains zero elements.

Then you call this:

            uns.add(i,u)

and we can infer from the exception message that i is 1. Of course, 1 is outside of the current indexable range of the list, and so you get that exception.

If your intent is that uns should be initialized with 10 elements, then you need to "fill" the array after creating it; e.g.

            for (int ii = 0; ii < 10; ii++) {
                uns.add(someValue);
            }

(There are 3rd party library methods that will create a list filled with with a given value. But for something this simple I wouldn't go hunting ...)

OTHER TIPS

An ArrayList is not sparse. You cannot add items for random positions that haven't been set yet. You can only insert items between position 0 and size(). This is documented behavior:

Throws:
IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())

Your code tries to add an item to position 1, while position 0 hasn't been set yet (the size() is 0). You need to just use add(item), or maybe you need to check your assumptions regarding the values of i.

Initializing an ArrayList using new ArrayList(10) does not define the size, only the initial capacity!

Just use

uns.add(u);

Instead of

uns.add(i,u);

You are receiving the error because you are trying to replace the element in the list at position i when there are no elements at that position yet.

You create an empty ArrayList with new ArrayList(10). This array list has an initial capacity of 10 elements, but it is still empty as long as you don't add anything into it.

When you access the ArrayList with i > 0, then you will get the exception.

Try to use instead:

uns.add(u)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top