Hi, I have written two classes. One is a pixel grabbing class the other allows the user to open and display an image. What I now want to be able to do is use the image which the use opened in the RunProgram class and grab pixels from it using the grabPixels class. I have tried many different things but it doesn't work. Could someone please help?

Thank you

package finalproject;

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;


/**
 *
 */
public class GrabPixels {
    public BufferedImage img;
    public RunProgram run;
    static public int[] pixels;
    static private int width;
    static private int height;

    public GrabPixels() {
        try {
            run = new RunProgram();
            
            img = ImageIO.read(new File("E:\\face1.jpg"));
        } catch (IOException ex) {
            Logger.getLogger(GrabPixels.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public GrabPixels(BufferedImage img) {
        this.img = img;
        try {
            img = ImageIO.read(new File("E:\\face1.jpg"));
        } catch (IOException ex) {
            Logger.getLogger(GrabPixels.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    public int[] getPixels() {
        if(img!=null) {
        width = img.getWidth();
        System.out.println("Width = " + width);
        height = img.getHeight();
        System.out.println("Height = " + height);
        pixels = new int[width * height];

        }
        PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixels, 0, width);
        try {
            pg.grabPixels();
            for (int i = 0; i < pixels.length; i++) {
                int pix = pixels[i];
                pixels[i] = pix*2;
                System.out.println(pix);
                i++;
            }
        } catch (InterruptedException e) {
            System.err.println("interrupted waiting for pixels!");
            return null;
        }
        if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
            System.err.println("image fetch aborted or errored");
            return null;
        }
        return pixels;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public void setWidth(int width) {
        GrabPixels.width = width;
    }

    public void setHeight(int height) {
        GrabPixels.height = height;
    }

    public int[][] get2DPixels() {
        int[][] twoDPixels = null;
        TwoDArray twoD = new TwoDArray(pixels, img.getWidth(), img.getHeight());
        return twoDPixels;
    }

    
    public void convertToImage(TwoDArray pixels1, int width1, int height1){
        pixels1 = FFT.output;
        int n = 0;
        for (int i = 0; i <FFT.output.size; i++){
            for(int j = 0; j<FFT.output.size;j++){
                int []transformedPixels;
            }
        }
        Toolkit tk = Toolkit.getDefaultToolkit();
    }
    
    private static Image imageto;
    public static void main(String[] args) {

        BufferedImage image = null;
        try {
            // Read from a file
            image = ImageIO.read(new File("E:\\face1.jpg"));
            //System.out.println("it works");
            GrabPixels bi = new GrabPixels(image);
            int[][] two;
            int[] array;
            array = bi.getPixels();
            MemoryImageSource src = new MemoryImageSource(width, height, array, 0, width);
            // Use a label to display the image
            JFrame frame = new JFrame();
            JLabel label = new JLabel();
            imageto = label.createImage(src);
            ImageIcon im = new ImageIcon(imageto);
            label.setIcon(im);
            frame.getContentPane().add(label, BorderLayout.CENTER);
            frame.pack();
            frame.setVisible(true);
        } catch (IOException e) {
        }
    }
}
/*
 * RunProgram.java
 *
 * Created on 04 November 2008, 17:46
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package finalproject;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;

/**
 *
 */
public class RunProgram extends JPanel implements ActionListener, ChangeListener, MouseListener, MouseMotionListener {

    static JMenuBar bar;
    private JMenuItem fileOpen;
    private JPanel picture1;
    private JPanel picture2;
    private JFileChooser fileChooser;
    File file;
    private JLabel leftPic;
    private JLabel rightPic;
    JButton openButton;
    JSlider phaseSlider;
    JButton t;
    private JButton openRight,  openLeft;
    static JPanel contentPane;

    /** Creates a new instance of RunProgram */
    public RunProgram() {
        setLayout(new BorderLayout());
        bar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        bar.add(fileMenu);

        fileOpen = new JMenuItem("Open");
        fileMenu.add(fileOpen);
        fileOpen.addActionListener(this);

        //Create a panel and make it the content pane.
        contentPane = new JPanel(new BorderLayout());
        contentPane.setBorder(BorderFactory.createRaisedBevelBorder());

        //create panel to show original pictures
        picture1 = new JPanel();
        picture1.addMouseListener(this);
        picture1.addMouseMotionListener(this);
        picture1.setBorder(BorderFactory.createLoweredBevelBorder());
        picture1.setPreferredSize(new Dimension(250, 150));

        picture2 = new JPanel();
        picture2.addMouseListener(this);
        picture2.addMouseMotionListener(this);
        picture2.setBorder(BorderFactory.createLoweredBevelBorder());
        picture2.setPreferredSize(new Dimension(250, 150));

        JPanel controlPanel = new JPanel();
        controlPanel.setBorder(BorderFactory.createLoweredBevelBorder());
        controlPanel.setPreferredSize(new Dimension(800, 50));
        controlPanel.setLayout(new FlowLayout());

        openLeft = new JButton("<<<Open");
        openLeft.addActionListener(this);
        openRight = new JButton("Open>>>");
        openRight.addActionListener(this);
        picture1.add(openLeft);
        picture2.add(openRight);

        contentPane.add(picture1, BorderLayout.WEST);
        contentPane.add(picture2, BorderLayout.EAST);
        contentPane.add(bar, BorderLayout.NORTH);

    }

//    /** Listen to the slider. */
   public void stateChanged(ChangeEvent e) {
    }

    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == openLeft) {
            fileChooser = new JFileChooser(".");

            fileChooser.addChoosableFileFilter(new ImageFilter());
            fileChooser.setAcceptAllFileFilterUsed(false);

            fileChooser.setAccessory(new ImagePreview(fileChooser));
            int directory = fileChooser.showOpenDialog(RunProgram.this);
            if (directory == JFileChooser.APPROVE_OPTION) {

                file = fileChooser.getSelectedFile();
                largeImageL = new ImageIcon(file.getAbsolutePath());
                try {
                    tmpImage = ImageIO.read(new File(file.getAbsolutePath()));
                    grabbing = new GrabPixels(tmpImage);
                    pixArr = grabbing.getPixels();
                    src = new MemoryImageSource(grabbing.getWidth(), grabbing.getHeight(), pixArr, 0, grabbing.getWidth());
                    imageto = label.createImage(src);
                    im = new ImageIcon(imageto);
                } catch (IOException ex) {
                }
                leftPic = new JLabel(largeImageL);
                leftPic.setBorder(new OvalBorder());

                leftPic.setPreferredSize(new Dimension(largeImageL.getIconWidth(), largeImageL.getIconHeight()));
                picture1.add(leftPic);
            }
            picture1.revalidate();
            picture2.revalidate();
        }
        if (e.getSource() == openRight) {
        }
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }
    static GrabPixels grabbing;
    static int[] pixArr;
    static MemoryImageSource src;
    static JLabel label;
    static ImageIcon im;

    public static void main(String[] args) {

        JFrame frame = new JFrame("ImageViewer");
        frame.add(new RunProgram());
        frame.setSize(500, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setJMenuBar(bar);
        frame.setContentPane(contentPane);
        frame.setVisible(true);

        JFrame frame1 = new JFrame();
        label = new JLabel();
        label.setIcon(im);
        frame1.getContentPane().add(label, BorderLayout.CENTER);
        frame1.pack();
        frame1.setVisible(true);
    }
    private static Image imageto;

    public void mouseDragged(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }
    int x, y;
    int radius = 5;
    ImageIcon largeImage;
    ImageIcon largeImageL;
    ImageIcon largeImageR;
    static BufferedImage tmpImage;
    Point startPoint;

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }
}

I have tried to use the following, however I keep getting a this error:
Exception in thread "main" java.lang.NullPointerException
at finalproject.GrabPixels.main(GrabPixels.java:132)

image = ImageIO.read(new File(run.file.getAbsolutePath()));

Could anyone please direct me to what I am doing wrong?

Thank you

Its just that the line in question:
image = ImageIO.read(new File(run.file.getAbsolutePath()));
doesn't seem to appear anywhere in the posted code

Hi sorry the code is new I hadn't actually updated the code. It replaces

image = ImageIO.read(new File("c:\\face1.jpg"));

Thank you

In the second class there is this line Whig I thought did that .

file = fileChooser.getSelectedFile();

It's hard to follow your code, but, if you're running main in GrabPixels, the very first thing it does is to read the file, before calling anything else that could set the file variable.
Either way, if you have a nullpointer in a given statement, try to print all the variables immediately before it to see which is null.
It may help to clean the code up and post the current version with code=java tags

Hi, I've tried to clean it up the best I could. To explain more clearly, the RunProgram class opens a frame and there is a button open which once clicked allows the user to select an image file to open in a JPanel. The GrabPixels class calls RunProgram and then tries to get the image which has been selected.

image = ImageIO.read(new File(run.file.getAbsolutePath()));

once this has been done, the pixels are extracted, modified and then the new image is redisplayed on a new frame. I have move the code around so in the main RunProgram is first called before grabPixels. I am not at a computer with netbeans at the moment. When I debugged it last night the problem was the variable file was empty therefore the line

image = ImageIO.read(new File(run.file.getAbsolutePath()));

has a problem which I can't seem to identify.

RunProgram

}/*
 * RunProgram.java
 *
 */
package finalproject;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.*;

/**
 *
 */
public class RunProgram extends JFrame implements ActionListener, ChangeListener, MouseListener, MouseMotionListener {

    static JMenuBar bar;
    private JMenuItem fileOpen;
    private JPanel picture1;
    private JPanel picture2;
    private JFileChooser fileChooser;
    File file;
    private JLabel leftPic;
    private JLabel rightPic;
    JButton openButton;
    JSlider phaseSlider;
    JButton t;
    private JButton openRight,  openLeft;
    ImageIcon largeImage;
    ImageIcon largeImageL;
    ImageIcon largeImageR;

    /** Creates a new instance of RunProgram */
    public RunProgram() {
        setLayout(new BorderLayout());
        bar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        bar.add(fileMenu);

        //create panel to show original pictures
        picture1 = new JPanel();
        picture1.addMouseListener(this);
        picture1.addMouseMotionListener(this);
        picture1.setBorder(BorderFactory.createLoweredBevelBorder());
        picture1.setPreferredSize(new Dimension(250, 150));

        picture2 = new JPanel();
        picture2.addMouseListener(this);
        picture2.addMouseMotionListener(this);
        picture2.setBorder(BorderFactory.createLoweredBevelBorder());
        picture2.setPreferredSize(new Dimension(250, 150));

        JPanel controlPanel = new JPanel();
        controlPanel.setBorder(BorderFactory.createLoweredBevelBorder());
        controlPanel.setPreferredSize(new Dimension(800, 50));
        controlPanel.setLayout(new FlowLayout());

        openLeft = new JButton("<<<Open");
        openLeft.addActionListener(this);
        openRight = new JButton("Open>>>");
        openRight.addActionListener(this);
        picture1.add(openLeft);
        picture2.add(openRight);

        contentPane.add(picture1, BorderLayout.WEST);
        contentPane.add(picture2, BorderLayout.EAST);
        contentPane.add(bar, BorderLayout.NORTH);

    }

   public void stateChanged(ChangeEvent e) {
    }

    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == openLeft) {
            fileChooser = new JFileChooser(".");

            fileChooser.addChoosableFileFilter(new ImageFilter());
            fileChooser.setAcceptAllFileFilterUsed(false);

            fileChooser.setAccessory(new ImagePreview(fileChooser));
            int directory = fileChooser.showOpenDialog(RunProgram.this);
            if (directory == JFileChooser.APPROVE_OPTION) {

                file = fileChooser.getSelectedFile();
                largeImageL = new ImageIcon(file.getAbsolutePath());
                leftPic = new JLabel(largeImageL);
                leftPic.setBorder(new OvalBorder());

                leftPic.setPreferredSize(new Dimension(largeImageL.getIconWidth(), largeImageL.getIconHeight()));
                picture1.add(leftPic);
            }
            picture1.revalidate();
            picture2.revalidate();
        }
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseDragged(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }
}

GrabPixels

package finalproject;

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.*;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;


/**
 *
 */
public class GrabPixels {
    public BufferedImage img;
    public RunProgram run;
    static public int[] pixels;
    static private int width;
    static private int height;

    public GrabPixels() {
        try {
            run = new RunProgram();
            
            img = ImageIO.read(new File("E:\\face1.jpg"));
        } catch (IOException ex) {
            Logger.getLogger(GrabPixels.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public GrabPixels(BufferedImage img) {
        this.img = img;
        try {
            img = ImageIO.read(new File("E:\\face1.jpg"));
        } catch (IOException ex) {
            Logger.getLogger(GrabPixels.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    public int[] getPixels() {
        if(img!=null) {
        width = img.getWidth();
        System.out.println("Width = " + width);
        height = img.getHeight();
        System.out.println("Height = " + height);
        pixels = new int[width * height];

        }
        PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixels, 0, width);
        try {
            pg.grabPixels();
            for (int i = 0; i < pixels.length; i++) {
                int pix = pixels[i];
                pixels[i] = pix*2;
                System.out.println(pix);
                i++;
            }
        } catch (InterruptedException e) {
            System.err.println("interrupted waiting for pixels!");
            return null;
        }
        if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
            System.err.println("image fetch aborted or errored");
            return null;
        }
        return pixels;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public void setWidth(int width) {
        GrabPixels.width = width;
    }

    public void setHeight(int height) {
        GrabPixels.height = height;
    }
    
    private static Image imageto;
    public static void main(String[] args) {

        BufferedImage image = null;
        RunProgram prog = new RunProgram();
        prog.setSize(1100, 900);
        prog.show();

        try {
            // Read from a file
            image = ImageIO.read(new File(run.file.getAbsolutePath()));
            //System.out.println("it works");
            GrabPixels bi = new GrabPixels(image);
            int[] array;
            array = bi.getPixels();
            MemoryImageSource src = new MemoryImageSource(width, height, array, 0, width);
            // Use a label to display the image
            JFrame frame = new JFrame();
            JLabel label = new JLabel();
            imageto = label.createImage(src);
            ImageIcon im = new ImageIcon(imageto);
            label.setIcon(im);
            frame.getContentPane().add(label, BorderLayout.CENTER);
            frame.pack();
            frame.setVisible(true);
        } catch (IOException e) {
        }
    }
}

Looks like your main(..) creates the GUI then immediately tries to open the file. Snag is, the user hasn't yet had a chance to pick menu or run the open dialog yet.

I did think it may be something to do with that. How could I get around that?

The code that opens the file etc needs to be in a method (pass the file as a parameter) that is called after the user has selected a file in the open dialog.

I have separated the opening of the file to a different method, however I still do not understand how I can actually get the paramter can be passed to GrabPixels before GrabPixels tries to initialise the variable?
The problem still lies here

image = ImageIO.read(new File(run.file.getAbsolutePath()));

This is the section that I changed:

public void actionPerformed(ActionEvent e) {

        if (e.getSource() == openLeft) {
            fileChooser = new JFileChooser(".");

            fileChooser.addChoosableFileFilter(new ImageFilter());
            fileChooser.setAcceptAllFileFilterUsed(false);

            fileChooser.setAccessory(new ImagePreview(fileChooser));
            int directory = fileChooser.showOpenDialog(RunProgram.this);
            if (directory == JFileChooser.APPROVE_OPTION) {
                file = fileChooser.getSelectedFile();
                openFile(file);
            }
            picture1.revalidate();
            picture2.revalidate();
        }
    }

    public File openFile(File f) {
        try{
        tmpImg = ImageIO.read(new File(file.getAbsolutePath()));
        largeImageL = new ImageIcon(file.getAbsolutePath());
        leftPic = new JLabel(largeImageL);
        leftPic.setBorder(new OvalBorder());
        }catch(IOException ex) {
            
        }

        leftPic.setPreferredSize(new Dimension(largeImageL.getIconWidth(), largeImageL.getIconHeight()));
        picture1.add(leftPic);
        return file;
    }

YOur code looks better now. You don't need to return the File from the new method, just leave the return s void. And
read(new File(file.getAbsolutePath()))
is just a long way to say
read(file)
Now all the other GrabPixels stuff needs to be called from in here instead of being in the GrabPixels constructor.

Thank you. It works now. However I now have another class which manipulates the image passed through from GrabPixels and it too is having a similar problem. I will start a new thread and post the question. many thanks for your help on this one.

This question has already been answered. Start a new discussion instead.