【问题标题】:Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0线程“AWT-EventQueue-0”中的异常 java.lang.ArrayIndexOutOfBoundsException: 0
【发布时间】:2012-04-06 16:12:27
【问题描述】:

在这个程序中,我得到 Array index out of bound 异常,我不知道它发生在哪里。这个程序从另一个程序已经存储的图像中检索数据(这里没有解释 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);
    }
    }
}

【问题讨论】:

  • 如需尽快获得更好的帮助,请发布SSCCE。顺便说一句throws Excenter code hereeption { WTF?!?这是 Java 吗?
  • 你可以放一些 System.out.println("") 语句来查看你的代码在哪一行如果没问题,哪一行抛出错误。
  • @AndrewThompson 在“例外”一词中是“在此处输入代码”:Exc|enter code here|eption

标签: java image swing file-io indexoutofboundsexception


【解决方案1】:

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

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

和:

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

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

来自File 的 JavaDoc:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多