1

in this program i m getting Array index out of bound exception, and i could not know where it is happening.This program retrieves the data from image already stored by some another program(Encrypt.java not explained here). thank you for your help

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); } } } 
3
  • 2
    For better help sooner, post an SSCCE. BTW throws Excenter code hereeption { WTF?!? Is this Java? Commented Mar 22, 2012 at 4:59
  • You can put some System.out.println("") statements to see upto which line your code if fine and what line throws the error. Commented Mar 22, 2012 at 5:07
  • @AndrewThompson It is "enter code here" within the word "Exception": Exc|enter code here|eption Commented Jun 17, 2013 at 3:11

1 Answer 1

3

I see two arrays in your code:

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

and:

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

Both arrays are sized based on a call to File.length()

From the JavaDoc for File:

The length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist. Some operating systems may return 0L for pathnames denoting system-dependent entities such as devices or pipes.

So my guess is that one of those files is either empty or does not exist.

(It would help diagnose the problem more decisively if you posted a complete stack trace, not just the exception message).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.