0

i have messages error "java.lang.IndexOutOfBoundsException: Index: 4, Size: 4" but every i build and run from begin its normal untill i insert data again i have messages error the same with number index diffrent.... can tell me to fix it ?

 public Barang barang;
public List <Barang> barangs;

public static BarangView getInstanceBarangView() {
    if (barangView == null) {
        barangView = new BarangView();
    }
    return barangView;
}

   public void refreshTable(){
    barangs = App.barangService().getSemuaBarang();
    tabelBarang.setModel(new BarangAbstractTableModel(barangs));
}

    public class BarangTableSelectionListener implements ListSelectionListener{  
        public void valueChanged(ListSelectionEvent e) {
        if(tabelBarang.getSelectedRow()>=0){                       
            barang = barangs.get(tabelBarang.getSelectedRow());
            barang = App.barangService().getBarang(barang.getIdBarang());

            idBarangTerpilih = barang.getIdBarang();            
            System.out.println("nim yang di pilih :"+idBarangTerpilih);
        }
    }
}

and the Error indicates to line barang = barangs.get (tableBarang.getSelectedRow ());

jitendra varshney
  • 3,484
  • 1
  • 21
  • 31
  • Could you share your full code? Somehow, your barangs list does not have the element you inserted when you try to get selected row – Adem İlhan Jul 19 '17 at 05:50
  • getSelectedRow() returns 4 because it has added 4 elements [1...4]. But when you use the get() method, it expects the array indexing from [0...3] – worker_bee Jul 19 '17 at 05:55
  • Can you please share the complete code? Where did you insert the elements? – yaswanth Jul 19 '17 at 06:16
  • if(barangs.size()> tabelBarang.getSelectedRow()){ barang = barangs.get(tabelBarang.getSelectedRow); } – Ihdina Jul 19 '17 at 06:18

2 Answers2

-1

Is it simply an offset by 1 error. Does your getSelectedRow start from index of 1?

doddi76
  • 61
  • 3
-1

Indexing starts from 0 to lenght-1. So if the size of the list is 4 then indexing will be like 0,1,2,3.

So if you want to access the 4th element in list then it is at index 3, not at 4.

gaurav sk
  • 46
  • 1
  • 6