Results 1 to 16 of 16
Like Tree5Likes
  • 1 Post By Fubarable
  • 1 Post By Fubarable
  • 1 Post By Fubarable
  • 1 Post By Fubarable
  • 1 Post By Fubarable

Thread: java.lang.ArrayIndexOutOfBoundsException

  1. #1
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default java.lang.ArrayIndexOutOfBoundsException

    hi all m having problem deleting table row from button in jtable .here is my error when i press on delete button this code is similar to my original code . please i need immediate feedback m stuck in this error from days.this is the error
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 0
    	at java.util.Vector.removeElementAt(Vector.java:554)
    	at javax.swing.table.DefaultTableModel.removeRow(DefaultTableModel.java:463)
    	at javaapplication8.ButtonEditorTest.selectedCol(ButtonEditorTest.java:57)
    	at javaapplication8.ButtonEditor.buttonPressed(ButtonEditor.java:60)
    	at javaapplication8.ButtonEditor.getTableCellEditorComponent(ButtonEditor.java:23)
    	at javax.swing.JTable.prepareEditor(JTable.java:5790)
    	at javax.swing.JTable.editCellAt(JTable.java:3515)
    	at javax.swing.plaf.basic.BasicTableUI$Handler.adjustSelection(BasicTableUI.java:1108)
    	at javax.swing.plaf.basic.BasicTableUI$Handler.mousePressed(BasicTableUI.java:1038)
    	at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:280)
    	at java.awt.Component.processMouseEvent(Component.java:6501)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    	at java.awt.Component.processEvent(Component.java:6269)
    	at java.awt.Container.processEvent(Container.java:2229)
    	at java.awt.Component.dispatchEventImpl(Component.java:4860)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Component.dispatchEvent(Component.java:4686)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4489)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    	at java.awt.Container.dispatchEventImpl(Container.java:2273)
    	at java.awt.Window.dispatchEventImpl(Window.java:2713)
    	at java.awt.Component.dispatchEvent(Component.java:4686)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    	at java.awt.EventQueue.access$000(EventQueue.java:101)
    	at java.awt.EventQueue$3.run(EventQueue.java:666)
    	at java.awt.EventQueue$3.run(EventQueue.java:664)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:680)
    	at java.awt.EventQueue$4.run(EventQueue.java:678)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)


    Java Code:
    import java.awt.Component;
    import java.util.EventObject;
    
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    import javax.swing.JTable;
    import javax.swing.event.CellEditorListener;
    import javax.swing.event.ChangeEvent;
    import javax.swing.table.TableCellEditor;
    
    public class ButtonEditor extends JButton implements TableCellEditor {
        public ButtonEditor() {
            super("Button");       
        }
    
        public Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected, int row, int column) {
            buttonPressed(table, row, column);
            return this;
        }
    
        public void cancelCellEditing() {
            System.out.println("Cancel");
        }
    
        public boolean stopCellEditing() {       
            return true;
        }
    
        public Object getCellEditorValue() {
            return null;
        }
    
        public boolean isCellEditable(EventObject anEvent) {
            return true;
        }
    
        public boolean shouldSelectCell(EventObject anEvent) {
            return true;
        }
    
        public void addCellEditorListener(CellEditorListener l) {
        }
    
        public void removeCellEditorListener(CellEditorListener l) {
        }
       
        protected void fireCellEditing(ChangeEvent e){
           
        }
       
        private void buttonPressed(JTable table, int row, int column){
            JOptionPane.showMessageDialog(table, "Pressed at " + row + "        " + column);
            ButtonEditorTest bet=new ButtonEditorTest();
            bet.selectedCol(row);
        }
    
    }
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.HeadlessException;
    
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import sun.swing.table.DefaultTableCellHeaderRenderer;
    
    public class ButtonEditorTest extends JFrame {
         JTable jTable1 = new JTable();
    
        /**
        * @throws HeadlessException
        */
         javax.swing.table.DefaultTableModel defaultTableModel = new javax.swing.table.DefaultTableModel();
        public ButtonEditorTest() throws HeadlessException {
            super();
           
          jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
                    {"asdf", "asdf", "asdf", "Delete", "Update"},
                    {"asdf", "asdf", "asdf", "Delete", "Update"},
                    {"asdf", "asdf", "asdf", "Delete", "Update"},
                    {"asdf", "asdf", "asdf", "Delete", "Update"}
                },
                new String [] {
                    "Sr.no", "Book Title", "Author Name", "Delete", "Edit"
                }
            ));
                 
            JScrollPane scroll = new JScrollPane(jTable1);
           
            jTable1.getColumnModel().getColumn(3).setCellRenderer(new ButtonRenderer());
            jTable1.getColumnModel().getColumn(3).setCellEditor(new ButtonEditor());
            jTable1.getColumnModel().getColumn(4).setCellRenderer(new ButtonRenderer());
            jTable1.getColumnModel().getColumn(4).setCellEditor(new ButtonEditor());
           
            getContentPane().add(scroll, BorderLayout.CENTER);       
            pack();
            DefaultTableCellHeaderRenderer dtchr=new DefaultTableCellHeaderRenderer();
        }
     public void selectedCol(int row){
        int rowCount= defaultTableModel.getRowCount();    
       for(int i=0;i<rowCount;i++ ){
            //defaultTableModel.removeRow(0);
            defaultTableModel.removeRow(jTable1.getSelectedRow());
            //System.out.println(i);
       }
    
      jTable1.setModel(defaultTableModel);
    defaultTableModel.removeRow(row);
    System.out.println("deleting");
    defaultTableModel.removeRow(jTable1.getSelectedRow());
    System.out.println("deleted");
    
     }
        public static void main(String[] args) {
            final JFrame f = new ButtonEditorTest();
            f.setDefaultCloseOperation(EXIT_ON_CLOSE);
           
            EventQueue.invokeLater(new Runnable(){
    
                public void run() {
                    f.setVisible(true);
                }});
        }
    
    }
    Java Code:
    import java.awt.Component;
    
    import javax.swing.JButton;
    import javax.swing.JTable;
    import javax.swing.table.TableCellRenderer;
    
    public class ButtonRenderer extends JButton implements TableCellRenderer {
    
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {
            String label = (value ==null) ? "" : value.toString();
        setText( label );
            
            return this;
        }
    }
    help please

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: java.lang.ArrayIndexOutOfBoundsException

    Look at the line causing the error and place several System.out.println's in this region. You'll see that you're trying to remove items from a vector (the tablemodel) that don't exist.

    Why do you have a for loop there anyway?
    Last edited by Fubarable; 09-29-2011 at 02:01 PM.
    rajasohaibmaroof likes this.

  3. #3
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: java.lang.ArrayIndexOutOfBoundsException

    i have this for loop there because i was just trying different options given in one site .are u suggesting that i dnt have a row there ?? but i have a row there when ckick on the button in shows that i have clicked on a particular row and when i pass this row as an argument in remove row it gives me error. i should be deleting it i do not know how to solve this problem can u please suggest me what should i do there

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: java.lang.ArrayIndexOutOfBoundsException

    Quote Originally Posted by rajasohaibmaroof View Post
    i have this for loop there because i was just trying different options given in one site .
    And this is the danger when you borrow code without understanding it. You shouldn't do this.

    are u suggesting that i dnt have a row there ?? but i have a row there when ckick on the button in shows that i have clicked on a particular row and when i pass this row as an argument in remove row it gives me error. i should be deleting it i do not know how to solve this problem can u please suggest me what should i do there
    No, I'm suggesting that you write your own code and think through what you want it to do. That for loop has no logical reason for being there. How you write your code is up to you and will depend on your needs.

    If you need more specific help, consider creating and posting an SSCCE that we can run and modify.
    Last edited by Fubarable; 09-29-2011 at 03:12 PM.
    rajasohaibmaroof likes this.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: java.lang.ArrayIndexOutOfBoundsException

    You know that you are creating more than one ButtonEditorTest objects, right? One that is displayed, and created here:

    Java Code:
       public static void main(String[] args) {
          final JFrame f = new ButtonEditorTest();
          f.setDefaultCloseOperation(EXIT_ON_CLOSE);
    
          EventQueue.invokeLater(new Runnable() {
    
             public void run() {
                f.setVisible(true);
             }
          });
       }

    And one that is create in a JButton's handler here:

    Java Code:
       private void buttonPressed(JTable table, int row, int column) {
          JOptionPane.showMessageDialog(table, "Pressed at " + row + "        "
                + column);
          ButtonEditorTest bet = new ButtonEditorTest();
          bet.selectedCol(row);
       }
    This second ButtonEditorTest serves no purpose other than to contribute to your error. Trying to remove a row of a non-displaying JTable doesn't make sense. Why not remove the row the JTable that is actually being shown?
    rajasohaibmaroof likes this.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: java.lang.ArrayIndexOutOfBoundsException

    I would get the original ButtonEditorTest object and call my method on it:

    Java Code:
       private void buttonPressed(JTable table, int row, int column) {
          JOptionPane.showMessageDialog(table, "Pressed at " + row + "        "
                + column);
    
          // !!
          ButtonEditorTest btnEditorTest = (ButtonEditorTest) SwingUtilities
                .getWindowAncestor(table);
          btnEditorTest.selectedCol(row);
          //!! ButtonEditorTest bet = new ButtonEditorTest();
          //!! bet.selectedCol(row);
       }

    I would make sure that the DefaultTableModel I'm working on is the same one as is being displayed in the JTable:

    Java Code:
    public class ButtonEditorTest extends JFrame {
       //!! JTable jTable1 = new JTable();
       
       //!!
       private DefaultTableModel defaultTableModel = (new DefaultTableModel(
             new Object[][] { { "asdf", "asdf", "asdf", "Delete", "Update" },
                   { "asdf", "asdf", "asdf", "Delete", "Update" },
                   { "asdf", "asdf", "asdf", "Delete", "Update" },
                   { "asdf", "asdf", "asdf", "Delete", "Update" } }, new String[] {
                   "Sr.no", "Book Title", "Author Name", "Delete", "Edit" }));
       private JTable jTable1 = new JTable(defaultTableModel); //!!
    
       
       public ButtonEditorTest() throws HeadlessException {
          super();
    
          //!! jTable1.setModel(defaultTableModel);

    And I would only delete the row of interest, nothing more:

    Java Code:
       public void selectedCol(int row) {
          // int rowCount = defaultTableModel.getRowCount();
          // !! for (int i = 0; i < rowCount; i++) {
          // // defaultTableModel.removeRow(0);
          // defaultTableModel.removeRow(jTable1.getSelectedRow());
          // // System.out.println(i);
          // }
    
          // jTable1.setModel(defaultTableModel);
          defaultTableModel.removeRow(row);
          System.out.println("deleting");
          //defaultTableModel.removeRow(jTable1.getSelectedRow());
          System.out.println("deleted");
    
       }
    rajasohaibmaroof likes this.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: java.lang.ArrayIndexOutOfBoundsException


  8. #8
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: java.lang.ArrayIndexOutOfBoundsException

    Quote Originally Posted by Norm View Post
    they are not replying me and saying cross posted. please people reply

  9. #9
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: java.lang.ArrayIndexOutOfBoundsException

    private void buttonPressed(JTable table, int row, int column) {
    JOptionPane.showMessageDialog(table, "Pressed at " + row + " "
    + column);

    // !!
    ButtonEditorTest btnEditorTest = (ButtonEditorTest) SwingUtilities
    .getWindowAncestor(table);
    btnEditorTest.selectedCol(row);
    //!! ButtonEditorTest bet = new ButtonEditorTest();
    //!! bet.selectedCol(row);
    }
    i did this thing
    public void selectedCol(int row) {
    // int rowCount = defaultTableModel.getRowCount();
    // !! for (int i = 0; i < rowCount; i++) {
    // // defaultTableModel.removeRow(0);
    // defaultTableModel.removeRow(jTable1.getSelectedRow ());
    // // System.out.println(i);
    // }

    // jTable1.setModel(defaultTableModel);
    defaultTableModel.removeRow(row);
    System.out.println("deleting");
    //defaultTableModel.removeRow(jTable1.getSelectedRow ());
    System.out.println("deleted");

    }
    this thing too
    public class ButtonEditorTest extends JFrame {
    //!! JTable jTable1 = new JTable();

    //!!
    private DefaultTableModel defaultTableModel = (new DefaultTableModel(
    new Object[][] { { "asdf", "asdf", "asdf", "Delete", "Update" },
    { "asdf", "asdf", "asdf", "Delete", "Update" },
    { "asdf", "asdf", "asdf", "Delete", "Update" },
    { "asdf", "asdf", "asdf", "Delete", "Update" } }, new String[] {
    "Sr.no", "Book Title", "Author Name", "Delete", "Edit" }));
    private JTable jTable1 = new JTable(defaultTableModel); //!!


    public ButtonEditorTest() throws HeadlessException {
    super();

    //!! jTable1.setModel(defaultTableModel);
    but i do not know how should i do this thing
    //!! this mark means that it should be not in the code but when i remove this
    JTable jTable1 = new JTable(); than i am not left with any jtable .thank u in advance for so much heelo please help me get through this error ill be very thank full to you sir :( i am desperately looking for help

  10. #10
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: java.lang.ArrayIndexOutOfBoundsException

    i went to dream in code they ask me to create SSCCE i created thios code if you will copy this code to Ur netbeans or eclipse it will run please have a look at it i do not know how to create SSCCE .i created this and i thought that it is SSCCE where as my real code is much more complex than this one .:(

  11. #11
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: java.lang.ArrayIndexOutOfBoundsException

    a for the understanding this goes goes i am just having problem with actionlistner this rest is ok . i and do not think that i would be able to get out of it without your help :( please help me

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: java.lang.ArrayIndexOutOfBoundsException

    I've already told you what you must do, and if it doesn't work, then yes, create an SSCCE. Also, you never notified us about the cross-posts until Norm forced your hand. Where else have you cross-posted this question? You need to show all links, else many of us will not waste our valuable time on this.
    rajasohaibmaroof likes this.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: java.lang.ArrayIndexOutOfBoundsException

    Check the other post. I think it's solved.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: java.lang.ArrayIndexOutOfBoundsException

    Quote Originally Posted by Norm View Post
    Check the other post. I think it's solved.
    Thanks. I won't waste my time with this one again.

  15. #15
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: java.lang.ArrayIndexOutOfBoundsException

    sorry i it wont happen again thank u a lot for giving me time from your precious time . the problem has been solved . i use various sites just to get it done quickly . u are very nice people indeed. i have this first experience of posting . but can u tell me how should i create SSCCE i think that creating a short program that depicts once problem is SSCCE.what i post above is SSCCE by my point of view . but again what is SSCCE

  16. #16
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: java.lang.ArrayIndexOutOfBoundsException

    ButtonEditorTest btnEditorTest = (ButtonEditorTest) SwingUtilities
    .getWindowAncestor(table);
    your this post help to solve my original program i am very thank full to you . sorry again . u are very good people

Similar Threads

  1. java.lang.ArrayIndexOutOfBoundsException: 1
    By Aparajita in forum New To Java
    Replies: 3
    Last Post: 08-21-2011, 12:26 AM
  2. java.lang.ArrayIndexOutOfBoundsException: 6
    By pyat77 in forum Java Applets
    Replies: 6
    Last Post: 03-06-2010, 02:12 PM
  3. java.lang.ArrayIndexOutOfBoundsException
    By mensa in forum Java 2D
    Replies: 7
    Last Post: 05-05-2008, 10:09 AM
  4. java.lang.ArrayIndexOutOfBoundsException
    By riccian in forum New To Java
    Replies: 0
    Last Post: 03-18-2008, 10:38 AM
  5. java.lang.ArrayIndexOutOfBoundsException
    By Marcus in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 06:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •