2012-03-22 104 views
1

在這個程序中,即時獲取數組索引超出了綁定異常,我不知道它發生在哪裏。這個程序從圖像中檢索數據已經由另一個程序存儲(Encrypt.java在這裏沒有解釋)。感謝您的幫助線程「AWT-EventQueue-0」的異常java.lang.ArrayIndexOutOfBoundsException:0

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

爲了更好地幫助越早,張貼[SSCCE(http://sscce.org/)。 BTW'拋出Excenter代碼hereeption {'WTF?!?這是Java嗎? – 2012-03-22 04:59:28

+0

您可以放置​​一些System.out.println(「」)語句來查看代碼的哪一行(如果沒有問題)以及哪一行會引發錯誤。 – 2012-03-22 05:07:53

+0

@AndrewThompson它是單詞「例外」中的「請在此輸入代碼」:'EXC |在此輸入代碼| eption' – gparyani 2013-06-17 03:11:30

回答

3

我看到你的代碼兩個數組:

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

和:

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

兩個數組都是基於呼叫尺寸爲File.length()

根據JavaDoc爲File

長度,以字節爲單位,此抽象路徑名, 或0L表示的文件的,如果該文件不存在。有些操作系統可以返回0L 爲路徑名錶示系統相關的實體,例如設備或 管道。

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

(這將有助於更果斷,如果你發佈一個完整的堆棧跟蹤,而不僅僅是異常消息診斷問題)。

+0

非常感謝你 – 2012-03-22 05:19:45

+0

@surendrasalke:如果它解決您的問題,您可以通過點擊接受這個答案[空對號](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235)。 – trashgod 2012-03-22 07:08:27

相關問題