java.lang.ArrayIndexOutOfBoundsException异常问题

在开发一个JAVA GUI 程序的时候遇到的问题。在建立一个AbstractTableModle时,使用的是POI导入的Excel表格数据,然后先为模型定义了属性组,六个,但在利用Vector<String>导入数据的时候,每一行只给了五个数据。这个问题我理解为在前面定义的属性组里的属性个数已与模型绑定,而实际给的数据少于这预先绑定的数量时,则线程在利用表格模型建表时会尝试访问空的元组单元,因此发生数组下标越界问题。

报错信息如下:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 5
at java.util.Vector.get(Unknown Source)
at kernel.TableModel.getValueAt(TableModel.java:75)
at javax.swing.JTable.getValueAt(Unknown Source)
at javax.swing.JTable.prepareRenderer(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JViewport.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1200(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)



java.lang.ArrayIndexOutOfBoundsException

04-01

[code=Java]rnpackage ProImage;rnrnimport java.awt.Image;rnimport java.awt.image.BufferedImage;rnimport java.awt.image.MemoryImageSource;rnimport java.awt.image.PixelGrabber;rnimport java.io.FileNotFoundException;rnimport java.io.FileOutputStream;rnimport java.io.IOException;rnrnimport javax.imageio.ImageIO;rnimport javax.swing.JFrame;rn/*rn * 1.PixelGrabber :将图片分解成象素存入int[]数组。alpha,green,red ,blue各占8位(bit)rn * rn 24-31 位表示 alpha 分量,16-23 位表示红色分量,8-15 位表示绿色分量,0-7 位表示蓝色分量。rn rn 00000000(alpha) 00000000(红) 00000000(绿) 00000000(蓝)rn rn 0xff=11111111=00000000 00000000 00000000 11111111rn rn 2.MemoryImageSource :将int[]数组合成图片.功能和PixelGrabber相反rn ImagemodImg = JFrame.createImage(rn new MemoryImageSource(w,h,colors,0,w))rnrn 3.rnrn * */rnpublic class AccordingPixArrayToCreateImage rnrn private int imageWidth;rn private int imageHeight;rn private int pixel[];rn rn private int[] blues;rn private int[] greens;rn private int[] reds;rn rn private Image image;rn rn private int index=0;rn public AccordingPixArrayToCreateImage()rn rn try rn loadImage();rn catch (Exception e) rn rn e.printStackTrace();rn rn rn GrabberPixelToArray();rn produceImage(blues);rn rn public static void main(String[] args)rn rn new AccordingPixArrayToCreateImage();rn rn rn public void loadImage() throws Exception//载入图片 同时初始化 同时保存图片宽度 高度 rn image=ImageIO.read(AccordingPixArrayToCreateImage.class.getResource("MM.jpg"));rn rn imageWidth=image.getWidth(null);rn imageHeight=image.getHeight(null);rn pixel=new int[imageWidth*imageHeight];rn blues=new int[imageWidth*imageHeight];rn greens=new int[imageWidth*imageHeight];rn reds=new int[imageWidth*imageHeight];rn System.out.println(imageWidth);rn System.out.println(imageHeight);rn System.out.println(imageWidth*imageHeight);rn rn rn rn public void GrabberPixelToArray()//抓取图片的像素点rn rn PixelGrabber grabber=new PixelGrabber(image,0,0,imageWidth,imageHeight,pixel,0,imageWidth);rn rn try rn grabber.grabPixels();//抓取rn System.out.println("-----"+pixel.length);rn catch (InterruptedException e) rn System.out.println("抓取图片像素出现异常了!!");rn e.printStackTrace();rn return;rn rn rn rn for(int i=0;i>24;//将像素值右移24位 即将最左边24位移除 该24位为红绿蓝得值 所以最后得到的结果为 00000000 00000000 00000000 11011011(alpha值)alpha值原先在左边 变成最右边rn int red=pixel>>16&0xff;//将像素值右移16位即将绿蓝得数值移除 左边补0 在与0xff进行与运算将alpha的值变成全0rn int green=pixel>>8&0xff;rn int blue=pixel&0xff;//蓝色像素位于最低位 所以不用移位 直接将其他位变为0rn rn alpha=alpha<<24;//alpha的值移位后 变成在最低位 所以要还原 左移24位rn red=red<<16|alpha;//还原 左移16位 绿蓝的值都变为0 在与alpha值或运算 将alpha值 添加到该值的最高位 所以该颜色 最后保留了alpha值 和红色值 绿蓝都变为0了rn green=green<<8|alpha;rn blue=blue|alpha;rn rn reds[index]=red;rn greens[index]=green;rn blues[index]=blue;rn rn index++;rn rn rn public void produceImage( int[] pixels)//根据存储像素的数组来初始化图片rn JFrame f=new JFrame();rn BufferedImage targetImage=(BufferedImage)f.createImage(new MemoryImageSource(imageWidth,imageHeight,pixels,0,imageWidth));rn rn try rn ImageIO.write(targetImage, "jpg", new FileOutputStream("c:/red.jpg"));rn catch (FileNotFoundException e) rn rn e.printStackTrace();rn catch (IOException e) rn rn e.printStackTrace();rn rn rnrnrn[/code]

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭