Results 1 to 4 of 4
  1. #1
    cvillejin is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default Java - Error in terminal window

    hi, i keep getting an error in terminal window after i compile, and i don't know how to fix it. can someone please help?

    the error is:

    the error is:

    java.lang.NegativeArraySizeException:
    In file MediaTools.java on line 329.
    It appears that the code is trying to create an array with a negative
    size. Remember, the array must have a size that is a positive number.

    This error is called a NegativeArraySizeException.

    at acm.util.MediaTools.getPixelArray(MediaTools.java: 329)
    at acm.graphics.GImage.getPixelArray(GImage.java:336)
    at WatermarkImages.watermark(WatermarkImages.java:63)
    at WatermarkImages.init(WatermarkImages.java:41)
    at acm.program.Program.start(Program.java:803)
    at acm.program.Program.start(Program.java:1279)
    java.lang.NegativeArraySizeException:
    In file MediaTools.java on line 329.
    It appears that the code is trying to create an array with a negative
    size. Remember, the array must have a size that is a positive number.

    This error is called a NegativeArraySizeException.

    at acm.util.MediaTools.getPixelArray(MediaTools.java: 329)
    at acm.graphics.GImage.getPixelArray(GImage.java:336)
    at WatermarkImages.watermark(WatermarkImages.java:62)
    at WatermarkImages.init(WatermarkImages.java:41)
    at acm.program.Program.start(Program.java:803)
    at acm.program.Program.start(Program.java:1279)
    Java Code:
    import acm.graphics.*;
    import acm.program.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.*;
    import javax.swing.*;
    
    // -------------------------------------------------------------------------
    /**
     *     
     *  @author  
     *  @version Spring, 2011 CS 1054 Intro to Java
     */
    public class WatermarkImages
        extends StudentTestableGraphicsProgram
    {
        //~ Instance/static variables .............................................
        public static final int APPLICATION_WIDTH = 1024;
        public static final int APPLICATION_HEIGHT = 600;
        public static final int TOLERANCE = 5;
        public static final int RGB_MAX = 255;
        
        GImage pic;
        GImage mark;
        //~ Methods ...............................................................
    
        // ----------------------------------------------------------
        /**
         * 
         */
        public void init()
        {
            addMouseListeners();
            String imgFile = chooseImage();
            pic = new GImage(imgFile);
            imgFile = chooseImage();
            mark = new GImage(imgFile);
    
            GImage marked = watermark(pic,mark);
            add(pic,0,0);
        }
        /**
         * 
         */
        public void run()
        {
            /*# add your statements here */
        }
        
        /**
         * Manipulates an image to show anything non-white and non-black in a watermark image transposed on it
         * 
         * @param img is the original image to be manipulated
         * @param water is the watermark to transpose onto img
         * @return a GImage that is the size of the smallest width and smallest height between the two images,
         *         with a watermark transposed onto the original
         */
        public GImage watermark(GImage img, GImage water)
        {
            int[][] imgArray = img.getPixelArray();
            int[][] waterArray = water.getPixelArray();
            int[][] combine;
            
            int width;
            int height;
            //find the smallest height...
            if(imgArray.length < waterArray.length)
            {
                height = imgArray.length;
            }
            else
            {
                height = waterArray.length;
            }
            //find the smallest width...
            if(imgArray[0].length < waterArray[0].length)
            {
                width = imgArray[0].length;
            }
            else
            {
                width = waterArray[0].length;
            }
            //create new 2D array the size of the smallest dimensions of both images...
            combine = new int[width][height];
            
            //traverse through 2D arrays...
            for(int i = 0; i < height; i++)
            {
                for(int j = 0; j < width; i++)
                {
                    int pixel = imgArray[i][j];
                    int red = GImage.getRed(pixel);
                    int green = GImage.getGreen(pixel);
                    int blue = GImage.getBlue(pixel);
                    
                    int maskPixel = waterArray[i][j];
                    int maskR = GImage.getRed(maskPixel);
                    int maskG = GImage.getGreen(maskPixel);
                    int maskB = GImage.getBlue(maskPixel);
                    
                    int markedPixel;
                    if( (maskR<TOLERANCE && maskG<TOLERANCE && maskB<TOLERANCE)                                   //black
                        || (maskR>(RGB_MAX-TOLERANCE) && maskG>(RGB_MAX-TOLERANCE) &&maskB>(RGB_MAX-TOLERANCE)) ) //or white
                    {
                        markedPixel = pixel;    
                    }
                    else
                    {
                        //transpose watermark shape onto original image...
                        int avg = (maskR + maskG + maskB)/3;
                        int newRed = (red+avg)/2;
                        int newGreen = (green+avg)/2;
                        int newBlue = (blue+avg)/2;
                        markedPixel = GImage.createRGBPixel(newRed,newGreen,newBlue);
                    }
                    combine[i][j] = markedPixel;
                }
            }
            return new GImage(combine);
        }
    
        /**
         * [Author: Kevin Buffardi] Saves a GImage to disk as a png file
         * @param picture A GImage picture that will be converted to png format and saved on disk
         * @param filename The name of the file to save (path optional, default to project folder.  e.g. "output.png")
         */
        public void saveImage(GImage picture, String filename)
        {
            try{
                File name = new File(filename);
                Image img = picture.getImage();
                BufferedImage buff = new BufferedImage(img.getWidth(null),img.getHeight(null),BufferedImage.TYPE_INT_RGB);
                Graphics graph = buff.getGraphics();
                graph.drawImage(img, 0, 0, null);
                graph.dispose();
                ImageIO.write(buff,"png",name);
            }
            catch(IOException e){
                System.out.println("Error when writing file: " + e.toString());
            }    
        }
        /**
         * [Author: Kevin Buffardi] Shows a file browser dialog so the user can choose a file.  
         * Only image files *should* be chosen, but this method does not prevent other types of files to be chosen.
         * @return A string representing the path and filename chosen (e.g. "C:\My Documents\image.jpg")
         */
        public String chooseImage() 
        {
            JFileChooser dialog = new JFileChooser();
            int result = dialog.showOpenDialog(this);
            if(result == JFileChooser.APPROVE_OPTION) {
                return dialog.getSelectedFile().getPath();
            }
            else
                return "";
        }
    }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    You've answered your question yourself...
    It appears that the code is trying to create an array with a negative
    size. Remember, the array must have a size that is a positive number.

    Without reading your code i'm going to point out this: if you are calculating the array index by some form of calculation which can give you +-number, use Math.abs(number) so that the number is always positive. But if that's not the case, then make sure you have your for-loops set up correctly.

  3. #3
    cvillejin is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default

    ehhh i found one error. there's 2 errors left apparently?

    i changed

    for(int j = 0; j < width; i++)

    to

    for(int j = 0; j <width; j++)

    and now I have the error

    java.lang.ArrayIndexOutOfBoundsException:
    In file WatermarkImages.java on line 119, which reads:

    }combine[i][j]=markedPixel;

    It seems that the code tried to use an illegal value as an index to an
    object. The code was trying to access an element at index 288 of an
    array (or other object) on that line. The size of the array may be
    less than 288. Keep in mind that if the array size is N, the biggest
    index you can access is N-1.

    This error is called an ArrayIndexOutOfBoundsException.

    at WatermarkImages.watermark(WatermarkImages.java:119 )
    at WatermarkImages.init(WatermarkImages.java:41)
    at acm.program.Program.start(Program.java:803)
    at acm.program.Program.start(Program.java:1279)

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Okay either this is really freaky or your compiler software is really good. Your answer is right there again.

    It seems that the code tried to use an illegal value as an index to an
    object. The code was trying to access an element at index 288 of an
    array (or other object) on that line. The size of the array may be
    less than 288. Keep in mind that if the array size is N, the biggest
    index you can access is N-1.

Similar Threads

  1. Mac Terminal
    By Java8 in forum Other IDEs
    Replies: 0
    Last Post: 01-16-2010, 07:19 PM
  2. Replies: 5
    Last Post: 05-25-2009, 10:03 PM
  3. Replies: 6
    Last Post: 02-24-2009, 03:50 PM
  4. Terminal
    By dj kourampies in forum New To Java
    Replies: 7
    Last Post: 01-07-2009, 04:57 PM
  5. Error logged from JDT Debug UI Window
    By tsc in forum Eclipse
    Replies: 1
    Last Post: 07-10-2008, 09:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •