I am developing a program. In this program I want to let the user to edit the jtable
(table has two column and user enter the data into second column). After that I want to store that edited data into integer type array(in this case I want to store second column data only).
getvalue
button code looks like below.
private void btntablesubmitMouseClicked(java.awt.event.MouseEvent evt) {
DefaultTableModel model = new DefaultTableModel();
int[] btime = new int[this.noofprocess];
tablebursttime.getModel();
for(int i=1; i <=this.noofprocess;i++){
btime[i] = Integer.parseInt( model.getValueAt(i,1).toString() );
System.out.println(btime[i]);
}
}
When I run this program with above code, there are some errors.
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 0
at java.util.Vector.elementAt(Vector.java:474)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:648)
at roundrobin.RoundRobinAlgorithm.btntablesubmitMouseClicked(RoundRobinAlgorithm.java:579)
at roundrobin.RoundRobinAlgorithm.access$1000(RoundRobinAlgorithm.java:17)
at roundrobin.RoundRobinAlgorithm$10.mouseClicked(RoundRobinAlgorithm.java:363)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6536)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2237)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2295)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4889)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4467)
at java.awt.Container.dispatchEventImpl(Container.java:2281)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:733)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
How do I prevent this error?
将for(int i=1; i <=this.noofprocess;i++){
更改for(int i=0; i <this.noofprocess;i++){
,原因是索引基于0
并且集合的大小从1
开始。
your Exception is coming from here
Integer.parseInt( model.getValueAt(i,2).toString() );
try to change your model object creation like below
DefaultTableModel model = (DefaultTableModel) tablebursttime.getModel();
Update
Try This
private void btntablesubmitMouseClicked(java.awt.event.MouseEvent evt) {
DefaultTableModel model = (DefaultTableModel) tablebursttime.getModel();
int[] btime = new int[this.noofprocess];
for(int i=0; i <this.noofprocess;i++){
btime[i] = Integer.parseInt( model.getValueAt(i+1,1).toString() );
System.out.println(btime[i]);
}
}
for(int i=0; i <this.noofprocess;i++){
btime[i] = Integer.parseInt( model.getValueAt(i,1).toString() );
System.out.println(btime[i]);
}
Value at (i,2) doesn't exist for 2 columns
You're not doing anything with the JTable
's model, and you will get an ArrayIndexOutOfBoundsException
. Try this:
private void btntablesubmitMouseClicked(java.awt.event.MouseEvent evt) {
TableModel model = tablebursttime.getModel();
int[] btime = new int[this.noofprocess];
for(int i=1; i <=this.noofprocess;i++){
btime[i-1] = Integer.parseInt( model.getValueAt(i,1).toString() );
System.out.println(btime[i-1]);
}
}
Your initialisation of model
is also the cause for the error. You should rather have DefaultTableModel model = (DefaultTableModel)tablebursttime.getModel()
. You can also add stopCellEditing()
to prevent a possible nullReference while the last cell of column 2 has focus.
tablebursttime.getCellEditor().stopCellEditing();
DefaultTableModel model = (DefaultTableModel)tablebursttime.getModel();
int[] btime = new int[this.noofprocess];
for(int i=0; i <this.noofprocess;i++){
btime[i] = Integer.parseInt(model.getValueAt(i,1).toString() );
System.out.println(btime[i]);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.