Results 1 to 15 of 15
  1. #1
    halim is offline Member
    Join Date
    Jul 2013
    Location
    Jakarta , Indonesia
    Posts
    11
    Rep Power
    0

    Question [ask] error when selected table

    I have 2 tables that are run at the same time using 2 JTabbedPane ..
    to Table 1 when clicked does not show up at all while the error when clicked on table 2 error will appear as below:

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.Vector.elementAt(Vector.java:430)
    at javax.swing.table.DefaultTableModel.getValueAt(Def aultTableModel.java:632)
    at javax.swing.JTable.getValueAt(JTable.java:2686)
    at appakademik.daftarmurid.upd(daftarmurid.java:632)
    at appakademik.daftarmurid.jTablemurid2MouseClicked(d aftarmurid.java:946)
    at appakademik.daftarmurid.access$700(daftarmurid.jav a:26)
    at appakademik.daftarmurid$10.mouseClicked(daftarmuri d.java:381)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEvent Multicaster.java:253)
    at java.awt.Component.processMouseEvent(Component.jav a:6266)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:602 8)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4574)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4247)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    BUILD SUCCESSFUL (total time: 7 seconds)

    i dont have any idea how to fix this error , anyone can help me?
    thanks,

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default Re: [ask] error when selected table

    Quote Originally Posted by halim View Post
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.Vector.elementAt(Vector.java:430)
    You're trying to get something from a Vector at index position -1; the JVM protests against it; something near:

    appakademik.daftarmurid.upd(daftarmurid.java:632)

    is the place to start looking for the bug ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    halim is offline Member
    Join Date
    Jul 2013
    Location
    Jakarta , Indonesia
    Posts
    11
    Rep Power
    0

    Default Re: [ask] error when selected table

    hmm ... I still do not understand to fix it ...
    What you mean is the placement of the data in the table that I have?
    any idea? i'm still learn it...

    kind regards,
    halim

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,380
    Rep Power
    5

    Default Re: [ask] error when selected table

    do you have any statements like:

    Vector [] = new Vector[2]; // or some number?

    Array indices go from 0 to arrayType.length - 1. They don't like negative indices.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    halim is offline Member
    Join Date
    Jul 2013
    Location
    Jakarta , Indonesia
    Posts
    11
    Rep Power
    0

    Default Re: [ask] error when selected table

    Quote Originally Posted by jim829 View Post
    do you have any statements like:

    Vector [] = new Vector[2]; // or some number?

    Array indices go from 0 to arrayType.length - 1. They don't like negative indices.

    Regards,
    Jim
    yes , i have..

    Java Code:
    private void LoadDatamurid() throws Exception
        {
            Statement st = cn.createStatement();
            //koding menghitung jumlah data
            ResultSet query= st.executeQuery("SELECT count(*)  AS jumlah FROM tsiswa b");
            txtcount.setText("");
            query.next();
            txtcount.setText(txtcount.getText()+ query.getString("jumlah"));
            query.close();
            //batas koding
            ResultSet rs = st.executeQuery("SELECT * FROM tsiswa a,tdaftar b WHERE b.kd_siswa = a.kd_siswa ORDER BY a.kd_siswa ASC");
            DefaultTableModel dtm = (DefaultTableModel) jTablemurid1.getModel();
            dtm.setRowCount(0);
            String [] data = new String[12];
            
            DefaultTableModel dtm1 = (DefaultTableModel) jTablemurid2.getModel();
            dtm1.setRowCount(0);
            String [] data1 = new String[9];
            
            int i = 1;
            
            while(rs.next())
            {
              
                data1[0] = rs.getString("kd_siswa");
                data1[1] = rs.getString("nm_siswa");
                data1[2] = rs.getString("kd_kls");
                data1[3] = rs.getString("jns_kls");
                data1[4] = rs.getString("sekolah");
                data1[5] = rs.getString("tgl_daftar");
                data1[6] = rs.getString("tgl_keluar");
                data1[7] = rs.getString("biaya");
                data1[8] = rs.getString("status_siswa");
             
                data[0] = rs.getString("kd_siswa");
                data[1] = rs.getString("nm_siswa");
                data[2] = rs.getString("tmpt_lhr");
                data[3] = rs.getString("tgl_lhr");
                data[4] = rs.getString("alamat");
                data[5] = rs.getString("tlp");
                data[6] = rs.getString("hp");
                data[7] = rs.getString("warga_negara");
                data[8] = rs.getString("gender");
                data[9] = rs.getString("agama");
                data[10] = rs.getString("kd_wali");
                data[11] = rs.getString("status_siswa");
                dtm.addRow(data);
                dtm1.addRow(data1);
                i++;
            }
            rs.close();
           
        }
    any idea?
    is my data isn't in place?

    regards,
    halim
    Last edited by halim; 07-29-2013 at 08:07 AM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: [ask] error when selected table

    Java Code:
            String [] data = new String[12];
    ...
            String [] data1 = new String[9];
    ...
            while(rs.next())
            {
                data1[0] = rs.getString("kd_siswa");
    ... etc
                data[0] = rs.getString("kd_siswa");
    ... etc
                dtm.addRow(data);
                dtm1.addRow(data1);
                i++;
            }
    Each row you are adding in that code is a reference to the exact same array.
    You never reinitialise 'data' or 'data1' in that loop, so all you are doing is overwriting the existing values each time.
    Now, this may not be the problem, but it is definitely *a* problem.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    halim is offline Member
    Join Date
    Jul 2013
    Location
    Jakarta , Indonesia
    Posts
    11
    Rep Power
    0

    Default Re: [ask] error when selected table

    Quote Originally Posted by Tolls View Post
    Java Code:
            String [] data = new String[12];
    ...
            String [] data1 = new String[9];
    ...
            while(rs.next())
            {
                data1[0] = rs.getString("kd_siswa");
    ... etc
                data[0] = rs.getString("kd_siswa");
    ... etc
                dtm.addRow(data);
                dtm1.addRow(data1);
                i++;
            }
    Each row you are adding in that code is a reference to the exact same array.
    You never reinitialise 'data' or 'data1' in that loop, so all you are doing is overwriting the existing values each time.
    Now, this may not be the problem, but it is definitely *a* problem.
    i still not understand what's mean , how to reinitialise 'data' or 'data1' in loop?
    can you give me the easy example? and at that loop its mean i want to show data of 2 table with the same unique code (its mean "kd_siswa")

    regards,
    halim
    Last edited by halim; 07-29-2013 at 08:54 PM.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: [ask] error when selected table

    Look:
    Java Code:
    String[] array = new String[3];
    array[0] = "Ug";
    ... this is the first time round your loop.
    ... the second time you do this:
    array[0] = "Og";
    Now, all you have done is change the value of the first entry in the array.
    Anything referencing that array will now see array[0] as "Og" and not "Ug".
    In your case that would be every row in your table.

    Since you don't want that, you need to create a new array each time round the loop...not use the same array(s).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    halim is offline Member
    Join Date
    Jul 2013
    Location
    Jakarta , Indonesia
    Posts
    11
    Rep Power
    0

    Default Re: [ask] error when selected table

    Quote Originally Posted by Tolls View Post
    Look:
    Java Code:
    String[] array = new String[3];
    array[0] = "Ug";
    ... this is the first time round your loop.
    ... the second time you do this:
    array[0] = "Og";
    Now, all you have done is change the value of the first entry in the array.
    Anything referencing that array will now see array[0] as "Og" and not "Ug".
    In your case that would be every row in your table.

    Since you don't want that, you need to create a new array each time round the loop...not use the same array(s).
    its mean i must build 2 table with different code and i can't build 2 table in 1 form?

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: [ask] error when selected table

    "you need to create a new array each time round the loop"

    That's it.
    Don't create the two arrays outside the while loop...create them inside the while loop.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    halim is offline Member
    Join Date
    Jul 2013
    Location
    Jakarta , Indonesia
    Posts
    11
    Rep Power
    0

    Default Re: [ask] error when selected table

    Quote Originally Posted by Tolls View Post
    "you need to create a new array each time round the loop"

    That's it.
    Don't create the two arrays outside the while loop...create them inside the while loop.
    Java Code:
    string [] data = new string [3]
    
    while(rs.next())
    {
    data[0]=rs.getString("kd_siswa");
    ... etc
    string [] data1 = new string[2];
    data[1]=rs.getString("kd_siswa1");
    }
    like this? am i right?

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: [ask] error when selected table

    Is that data[] created (ie is the 'new') inside the while loop or outside it?
    Because that is what determines whether you have a single row added multiple times (which is your current problem), or lots of different rows.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,669
    Rep Power
    5

    Default Re: [ask] error when selected table

    You still don't understand what your code is doing. Try writing out what it does. Lets say you have an array of two slots.

    Java Code:
    String[] data = new String[2];
    and you have a database table with two columns which you want to put in that array. The table also has two rows, something like this:

    Java Code:
    |column1|column2|
    ----------------
    |value1| value2|
    |value3| value4|
    So you fire your select query and you get a ResultSet. You then loop through the resultset using your while loop:

    Java Code:
    while(rs.next()) {
      ...
    }
    This while loop is going to loop 2 times since there are 2 rows in the database. So now we add in your remainder code:

    Java Code:
    while(rs.next()) {
      data[0]=rs.getString("column1");
      data[1]=rs.getString("column2");
    }
    This is what happens using this code.

    LOOP 1:
    data[0] contains null. data[0] is set to "value1"
    data[1] contains null. data[1] is set to "value2"

    LOOP 2:

    data[0] contains "value1". data[0] is set to "value3"
    data[1] contains "value2". data[1] is set to "value4"


    Do you get it now? The data from the first row in the database is lost because you overwrite it with the next row.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    halim is offline Member
    Join Date
    Jul 2013
    Location
    Jakarta , Indonesia
    Posts
    11
    Rep Power
    0

    Default Re: [ask] error when selected table

    Quote Originally Posted by Tolls View Post
    Is that data[] created (ie is the 'new') inside the while loop or outside it?
    Because that is what determines whether you have a single row added multiple times (which is your current problem), or lots of different rows.
    of the code that I created, I want to display data from two records in the database that I want to show the 2 tables are created in one form
    I am confused but I wish to make a code to click on the table
    so if I click on one of the tables on the form then they will automatically appear in the field on the form provided

    any ideas?
    regards,
    halim

  15. #15
    halim is offline Member
    Join Date
    Jul 2013
    Location
    Jakarta , Indonesia
    Posts
    11
    Rep Power
    0

    Default Re: [ask] error when selected table

    Quote Originally Posted by gimbal2 View Post
    You still don't understand what your code is doing. Try writing out what it does. Lets say you have an array of two slots.

    Java Code:
    String[] data = new String[2];
    and you have a database table with two columns which you want to put in that array. The table also has two rows, something like this:

    Java Code:
    |column1|column2|
    ----------------
    |value1| value2|
    |value3| value4|
    So you fire your select query and you get a ResultSet. You then loop through the resultset using your while loop:

    Java Code:
    while(rs.next()) {
      ...
    }
    This while loop is going to loop 2 times since there are 2 rows in the database. So now we add in your remainder code:

    Java Code:
    while(rs.next()) {
      data[0]=rs.getString("column1");
      data[1]=rs.getString("column2");
    }
    This is what happens using this code.

    LOOP 1:
    data[0] contains null. data[0] is set to "value1"
    data[1] contains null. data[1] is set to "value2"

    LOOP 2:

    data[0] contains "value1". data[0] is set to "value3"
    data[1] contains "value2". data[1] is set to "value4"


    Do you get it now? The data from the first row in the database is lost because you overwrite it with the next row.
    oh so so, but I'm confused its not there,
    what if I show the code of my application?

    excuse me if I do not understand your point,
    because I am still learning
    regards,
    halim

Similar Threads

  1. Replies: 0
    Last Post: 01-17-2013, 12:43 PM
  2. JButton to detect if cell in a table is selected
    By Levian in forum New To Java
    Replies: 5
    Last Post: 08-09-2011, 06:56 AM
  3. Replies: 0
    Last Post: 07-11-2008, 04:30 PM
  4. Replies: 0
    Last Post: 07-11-2008, 04:29 PM
  5. merging selected table cells using javascript
    By Renjini in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-17-2008, 01:04 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
  •