-2

I am trying to develop an image editor in Java currently using BufferedImage, JFrame, and JPanel but I am having trouble getting my buttons to interact with my BufferedImage that I read from a file. Here is my code:

   import java.awt.*;
   import java.awt.Graphics;

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



/**
This class just holds the main
*/
    public class ImageEditorDeluxe
   {
       public static void main(String[] args)
      {
         ProgramWindow p = new ProgramWindow();
         p.setBounds(100, 100, 500, 500);
         p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         p.setVisible(true);
        //  p.pack();
      }
   }

/**
This is where all of the JPanels will come together
*/
    class ProgramWindow extends JFrame  
   {
       ProgramWindow()
      {
         ImagePanel ip = new ImagePanel();
         ChooseFile cf = new ChooseFile();
         ButtonPanel bp = new ButtonPanel(ip.getImg());

         add(ip, BorderLayout.CENTER);
         add(cf, BorderLayout.SOUTH);
         add(bp, BorderLayout.WEST);
      }
   }

/**
This is where the image will be displayed
*/
    class ImagePanel extends JPanel
   {
      BufferedImage img;

       ImagePanel()
      {
         setBackground(Color.BLUE);  //to test
         final JButton button = new JButton ("Display picture");
         add(button);       

         ActionListener action = 
             new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
               {
                  if (e.getSource()==button)
                  {
                     try
                     {
                        img = ImageIO.read(ChooseFile.getFile());
                     }
                         catch(IOException f)
                        {
                           f.printStackTrace();
                        }

                     repaint();

                  }
               }

            };

         button.addActionListener(action);
      }

       public void paintComponent(Graphics g)
      {
         super.paintComponent(g);
         if (img != null)
            g.drawImage(img, 0, 0, this);
      }

       public void setImage(BufferedImage i)
      {
         img = i;
         repaint();
      }
        public BufferedImage getImg()
        {
            return img;
        }
   }

/**
This is where the JFileChooser will exist
*/

    class ChooseFile extends JPanel
   {
      static File file;
      JButton bOpen, bDisplay;
      BufferedImage img;


       ChooseFile()
      {
         setBackground(Color.GREEN); //to test
         bOpen = new JButton("Open File");
         add(bOpen);
      //    bDisplay = new JButton("Display File");
      //    add(bDisplay);
         final JFileChooser chooser = new JFileChooser();


         ActionListener action = 
             new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
               {
                //open file
                  if (e.getSource()==bOpen)
                  {
                     chooser.showOpenDialog(null);
                     file = chooser.getSelectedFile();
                  }
                //display file
               /*       if (e.getSource()== bDisplay)
                  {
                     try
                     {
                        img = ImageIO.read(file);
                     }
                         catch(IOException f)
                        {
                           f.printStackTrace();
                        }

                  }*/
               }

            };

         bOpen.addActionListener(action);
      //        bDisplay.addActionListener(action);
      }

    //returns String name of file
       static File getFile() throws IOException
      {
         return file;
      }
   }

    class ButtonPanel extends JPanel
   {
        JButton makeBlue;
        public static BufferedImage img;
        public static int width, height;
        //BufferedImage img;
       ButtonPanel(BufferedImage x)
      {

            img = x;
            int width, height;
         setBackground(Color.RED);
         makeBlue = new JButton("Make Blue");
         add(makeBlue);
         ActionListener action = 
             new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
               {
                        int width, height;
                  if (e.getSource()== makeBlue)
                  {
                            //img = x;
                         width = img.getWidth();
                             height = img.getHeight();
                             System.out.println(width + " " + height);
                     repaint();

                  }
               }

            };

         makeBlue.addActionListener(action);
      }
   }

The error is somewhere in the ButtonPanel class but I am not sure what it is. The error I am currently getting when I press the makeBlue button is this:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ButtonPanel$1.actionPerformed(ImageEditorDeluxe.java:185)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Any help would be appreciated. Thank you.

masonc15
  • 1,043
  • 2
  • 12
  • 19
  • ImageEditorDeluxe.java:185, what does that line have ? – perencia May 18 '14 at 19:15
  • You should know how to look at the exception stack trace and identify the failing line. Then it's a relatively simple matter to trace back from there and figure out why the pointer is null. – Hot Licks May 18 '14 at 19:21
  • I am pretty new with Java and this is an ambitious project. I do not know how to look at an exception stack trace or what that is. – masonc15 May 18 '14 at 19:22
  • Then do a little research. Knowing the basics of how to debug is a fundamental skill that you need to learn right away. Any time you start a new language or environment you should first find and bookmark suitable documentation and find out how to read the "call stack" in the case of an error. Java actually makes this latter problem very simple since it produces the stack trace by default. You should be able to look at it and understand the basics of it without any assistance, but if you do have a specific problem, come back and ask a *specific* question. – Hot Licks May 19 '14 at 02:58

1 Answers1

2

The stacktrace is pretty clear:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at ButtonPanel$1.actionPerformed(ImageEditorDeluxe.java:185)

Which boils down to this line of code:

width = img.getWidth();

in this bigger piece of code:

class ButtonPanel extends JPanel
{
    JButton makeBlue;
    public static BufferedImage img;
    public static int width, height;
    //BufferedImage img;
    ButtonPanel(BufferedImage x)
    {
    
        img = x;
        int width, height;
        setBackground(Color.RED);
        makeBlue = new JButton("Make Blue");
        add(makeBlue);
        ActionListener action = 
            new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    int width, height;
                    if (e.getSource()== makeBlue)
                    {
                        //img = x;
                        width = img.getWidth();
                        height = img.getHeight();
                        System.out.println(width + " " + height);
                        repaint();

                    }
                }

            };

        makeBlue.addActionListener(action);
    }
}

Looks like img is null. Use a debugger to find why and fix it accordingly.

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • I changed the code to this: `JButton makeBlue; BufferedImage img; ButtonPanel(BufferedImage x) { img = x; int width, height; ...` but now I get this error: `error: local variable width is accessed from within inner class; needs to be declared final width = img.getWidth();` – masonc15 May 18 '14 at 19:21
  • @masonc15 do the necessary debugging to find why your `img` is `null`. – Luiggi Mendoza May 18 '14 at 19:22
  • I am really not familiar with debugging and I am not aware of a clear fix to this problem. I know it has something to do with scope. – masonc15 May 18 '14 at 19:25
  • @masonc15 now you have a good reason to learn. – Luiggi Mendoza May 18 '14 at 19:25