Three-dimensional array

I want to store pixels of images in a 3D array and I did it like:

public class ImageProcessing {
    private BufferedImage image;
    int p = image.getWidth();
    int q = image.getHeight();
    int r = 3;
    private int[][][] data = new int[p][q][r];

      

then i get the pixel value from this image and store it in this array and i make the constructor like this:

public ImageProcessing(BufferedImage img){
        image = img;
        for(int x = 0; x < image.getWidth(); x++){
            for(int y = 0; y < image.getHeight(); y++){
                int px = image.getRGB(x,y);
                int alpha = (px >> 24) & 0xFF;
                int red = (px >> 16) & 0xFF;
                int green = (px >> 8) & 0xFF;
                int blue = px & 0xFF;
                data[x][y][0] = alpha;
                data[x][y][1] = red;
                data[x][y][2] = green;
                data[x][y][3] = blue;
            }
        }
    }

      

then I want to get it to convert the image to grayscale like this:

public BufferedImage greyscale(){   
        for(int x = 0; x < p ; x++){
            for(int y = 0; y < q ; y++){
                int avg = (data[x][y][1] + data[x][y][2] + data[x][y][3]/3);
                int grey = (data[x][y][0]<<24) + (avg<<16) + (avg<<8) + avg;
                image.setRGB(x, y, grey);
            }
        }
        return image;
    }

      

But when I run it I am showing the error as follows. I don't know what this means and how to solve it?

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ImageProcessing.<init>(ImageProcessing.java:6)
    at Main$1.run(Main.java:14)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

      

How to solve it? Any idea?

+3


source to share


1 answer


Obviously, this will be because

    private BufferedImage image;       // had not initialized image
    int p = image.getWidth();          // will be null
    int q = image.getHeight();         // same here
    int r = 3;
    private int[][][] data = new int[p][q][r];

      

You have not initialized BufferedImage image

, so it is currently pointed to NULL, and when you initialize p = image.getWidth();

and int q = image.getHeight();

then you have no values, so

private int[][][] data = new int[p][q][r];

This operator above will throw NE

.

You should just declare the array in the class and initialize it in the constructor of your initialized class image

.




Edit:

Check out the code below, initialize it BufferedImage

, then do the same and it works great !!

public class ArrayInitialize {
    public static void main(String ar[]){
        File file = new File("Image.jpg");
        BufferedImage img = new BufferedImage(240, 240, BufferedImage.TYPE_INT_ARGB);
        try {
            img = ImageIO.read(file);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int p = img.getWidth();          // will be null
        int q = img.getHeight();         // same here
        int r = 3;
        int count = 0;
        int[][][] data = new int[p][q][r];
        for(int i=0;i<p;i++){
            for(int j=0;j<q;j++){
                for(int k=0;k<r;k++){
                    count++;
                }
            }
        }
        System.out.println(count);
    }
}

      

I think after initialization BufferedImage

, if you get NullPointerException

then there is a problem with your method getImage()

, it may not work as expected.

And see comment

int r = 3 should be int r = 4, right. Each color has 4 components.

+2


source







All Articles