线程“AWT-EventQueue-0”中的异常java.lang.ArrayIndexOutOfBoundsException:0

时间:2012-03-22 04:41:08

标签: java image swing file-io indexoutofboundsexception

在这个程序中我得到了Array索引超出绑定的异常,我无法知道它在哪里发生。这个程序从已经由另一个程序存储的映像中检索数据(这里没有解释Encrypt.java)。谢谢你的帮助

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.math.BigInteger;


public class Decrypt extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static void main(String[] args) throws Exception {
    new Decrypt();
    }

    public Decrypt() throws Excenter code hereeption {
    setTitle("Color");
    setSize(300, 300);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(3);

    add(new PaintPanel(ImageIO.read(new File("src/outputImages/s2.bmp"))));
    setVisible(true);
    }

    class PaintPanel extends JPanel {

    /**
         * 
         */
        private static final long serialVersionUID = 1L;
    private BufferedImage image;

    PaintPanel(BufferedImage image) {
        this.image = image;
    }

    public void paintComponent(Graphics g) {
        File file = new File("src/input.txt");
        int length=0, insertCounter;
        String strFileContent="";
        try {
        //create FileInputStream object
        FileInputStream fin = new FileInputStream(file);

        /*
         * Create byte array large enough to hold the content of the file.
         * Use File.length to determine size of the file in bytes.
         */


        byte fileContent[] = new byte[(int)file.length()];

        /*
         * To read content of the file in byte array, use
         * int read(byte[] byteArray) method of java FileInputStream class.
         *
         */
        fin.read(fileContent);

        //create string from byte array
            strFileContent = new String(fileContent);

        System.out.println("File content : "+ strFileContent );

        BigInteger bi = new BigInteger(fileContent);
        // Format to binary
        strFileContent = bi.toString(2);      // 100100000111111110000
        length=strFileContent.length();      //thia variable contains the length of binary string
        //System.out.println(length+" File content in binary: "+ strFileContent );
        }
        catch(Exception e){
        System.out.println("File not found" + e);
        }

        File file1 = new File("input.txt");
        byte outputBytes[] = new byte[(int)file1.length()];
        insertCounter=0;
        int localCounter=0;
        byte tempbyte=0x00;
        for (int h = 0; h <=image.getHeight()-3; h=h+3) {
        for (int w = 0; w <=image.getWidth()-3; w=w+3) {
            int whiteParity=0;
            for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                int rgb = image.getRGB(w+j,h+ i);     
                if (rgb == -1)
                whiteParity++;
            }
            }
            /* if(WhitePArity==1 ||WhitePArity==3 ||WhitePArity==5 ||WhitePArity==7 ||WhitePArity==9){

               }*/

            if( image.getRGB(w+1,h+1)==-1){
            if(localCounter==0)
                tempbyte=(byte)(tempbyte|0x80);
            if(localCounter==1)
                tempbyte=(byte)(tempbyte|0x40);
            if(localCounter==2)
                tempbyte=(byte)(tempbyte|0X20);
            if(localCounter==3)
                tempbyte=(byte)(tempbyte|0x10);
            if(localCounter==4)
                tempbyte=(byte)(tempbyte|0x08);
            if(localCounter==5)
                tempbyte=(byte)(tempbyte|0x04);
            if(localCounter==6)
                tempbyte=(byte)(tempbyte|0x02);
            if(localCounter==7)
                tempbyte=(byte)(tempbyte|0x01);
            }
            localCounter++;

            if(localCounter==8){
            outputBytes[insertCounter]=tempbyte;
            insertCounter++;
            localCounter=0;
            tempbyte=(byte)(tempbyte & 0x00);
            }


        }

        String outputString=new String(outputBytes);
        String strFilePath = "output.txt";

        try{
            FileWriter fstream = new FileWriter(strFilePath);
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(outputString);
            //Close the output stream
            out.close();
        }
        catch(Exception e){
            System.out.println("File not found" + e);
        }

        }

        g.drawImage(image, 0, 0, this);
    }
    }
}

1 个答案:

答案 0 :(得分:3)

我在你的代码中看到两个数组:

byte fileContent[] = new byte[(int)file.length()];

byte outputBytes[] = new byte[(int)file1.length()];

两个数组的大小都是基于对File.length()

的调用

来自File的JavaDoc:

  

此抽象路径名表示的文件的长度(以字节为单位),   如果文件不存在,则返回0L。某些操作系统可能会返回0L   用于表示系统相关实体(如设备或)的路径名   管道

所以我的猜测是其中一个文件是空的或不存在。

(如果您发布完整的堆栈跟踪,而不仅仅是异常消息,这将有助于更具决定性地诊断问题。)