0

I made my own package to store my drafts of my programming assignment and I made my final draft. I copied and pasted the final draft into the actual package my professor gave, but it won't compile at all and gives me all these errors. I tried doing the Project > Clean, but it didn't help. I don't understand how the file works in one place but not the other.

Here's my code

package gui;


import filters.EdgeDetectFilter;
import filters.EdgeHighlightFilter;
import filters.Filter;
import filters.FlipHorizontalFilter;
import filters.FlipVerticalFilter;
import filters.GrayscaleFilter;
import filters.SharpenFilter;
import filters.SoftenFilter;
import image.PixelImage;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
//import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;


import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;    


public class SnapShopGUI {

    private final JFrame myFrame; 

    private final JPanel myMenuPanel;

    private final JPanel myTopPanel;

    private final JPanel myBottomPanel;

    private final JPanel myImagePanel;

    private JButton myOpen;

    private JButton mySave;

    private JButton myClose;

    private JFileChooser myFileChooser;

    private PixelImage myImage;

    private JLabel myLabel;

//  private String currentDirectory;

    private ArrayList<Filter> myFilters;

    private ArrayList<JButton> myFilterButtons;



    /*
     * Initializes all fields.
     */
    public SnapShopGUI() {
        myFrame = new JFrame("Test Layout");
        myFrame.getContentPane().setLayout(new BorderLayout());

        myMenuPanel = new JPanel(new BorderLayout());

        myTopPanel = new JPanel(new GridLayout(0, 1, 6, 6));
        myTopPanel.setBorder(new EmptyBorder(10, 10, 3, 10));

        myBottomPanel = new JPanel(new GridLayout(0, 1, 6, 6));         
        myBottomPanel.setBorder(new EmptyBorder(3, 10, 10, 10));  

        myImagePanel = new JPanel(new BorderLayout());

        myLabel = new JLabel();
//        final ImageIcon icon = new ImageIcon(getClass().getResource("seahawks_logo.gif")); 
//        myLabel.setIcon(icon);
        myFileChooser = new JFileChooser();
//      myFileChooser.setPreferredSize(new Dimension(500, 400));
        myFileChooser.setPreferredSize(new Dimension(600, 400));

        myFilterButtons = new ArrayList<JButton>();


    }
//    
    public void start() {

        myImagePanel.setBackground(Color.WHITE);

        myImagePanel.add(myLabel, BorderLayout.NORTH);

        createFilters();

        // BOTTOM MENU STUFF
        myBottomPanel.add(createOpenButton());
        myBottomPanel.add(createSaveButton());
        myBottomPanel.add(createCloseButton());

        myMenuPanel.add(myTopPanel, BorderLayout.NORTH);
        myMenuPanel.add(myBottomPanel, BorderLayout.SOUTH);     

        myFrame.add(myMenuPanel, BorderLayout.WEST);
        myFrame.add(myImagePanel);

        myFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        myFrame.pack();
        myFrame.setVisible(true);
        myFrame.setLocationRelativeTo(null); // put this last to center window
    }

    public void createFilters() {

        myFilters = new ArrayList<Filter>();
        myFilters.add(new EdgeDetectFilter());
        myFilters.add(new EdgeHighlightFilter());
        myFilters.add(new FlipHorizontalFilter());
        myFilters.add(new FlipVerticalFilter());
        myFilters.add(new GrayscaleFilter());
        myFilters.add(new SharpenFilter());
        myFilters.add(new SoftenFilter());

        for (Filter filter : myFilters) {
            myTopPanel.add(createFilterButton(filter));         
        }
    }

    public JButton createFilterButton(final Filter theFilter) {
        JButton fButton = new JButton(theFilter.getDescription());
//      fButton.setPreferredSize(new Dimension(120, 30));
        fButton.setEnabled(false);
        myFilterButtons.add(fButton);

        class FilterActionListener implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                theFilter.filter(myImage);
                myLabel.setIcon(new ImageIcon(myImage));

            }           
        }

        fButton.addActionListener(new FilterActionListener());

        return fButton;
    }
//    
    public JButton createOpenButton() {
        ImageIcon openButtonIcon = new ImageIcon(getClass().getResource("open.gif"));
        myOpen = new JButton("Open...", openButtonIcon);


        class OpenActionListener implements ActionListener {

            @Override
            public void actionPerformed(final ActionEvent e) {
                int returnVal = myFileChooser.showOpenDialog(myFrame);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = myFileChooser.getSelectedFile();
                    try {
                        myImage = PixelImage.load(file);
                        myLabel.setIcon(new ImageIcon(myImage));
                        myFrame.pack();
                    } catch (final IOException fileErr) {
                        JOptionPane.showMessageDialog(myFrame,
                                "The selected file did not contain an image!",
                                "Error!",
                                JOptionPane.ERROR_MESSAGE);
                    }
                    myFrame.setMinimumSize(myFrame.getSize());

                    for (JButton button : myFilterButtons) {
                        button.setEnabled(true);
                    }

                    mySave.setEnabled(true);
                    myClose.setEnabled(true);
                }           
            }
        }

        myOpen.addActionListener(new OpenActionListener());

        return myOpen;

    }

    public JButton createSaveButton() { 
        final ImageIcon saveButtonIcon = new ImageIcon(getClass().getResource("save.gif"));
        mySave = new JButton("Save As...", saveButtonIcon);
        mySave.setEnabled(false);

        class SaveActionListener implements ActionListener {

            @Override
            public void actionPerformed(final ActionEvent e) {
                myFileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
                int returnVal = myFileChooser.showSaveDialog(myFrame);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    try {
                        myImage.save(myFileChooser.getSelectedFile());
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }           
                }           
            }
        }

        mySave.addActionListener(new SaveActionListener());
        return mySave;      
    }

    public JButton createCloseButton() {
        final ImageIcon closeButtonIcon = new ImageIcon(getClass().getResource("close.gif"));
        myClose = new JButton("Close", closeButtonIcon);
        myClose.setEnabled(false);

        class CloseActionListener implements ActionListener {

            @Override
            public void actionPerformed(final ActionEvent e) {
                myLabel.setIcon(null);  
                for (JButton button : myFilterButtons) {
                    button.setEnabled(false);
                }
                myClose.setEnabled(false);
                mySave.setEnabled(false);
                myFrame.setMinimumSize(myMenuPanel.getSize());
                myFrame.pack();
            }       
        }

        myClose.addActionListener(new CloseActionListener());

        return myClose;
    }
}

And these are the errors I get. I don't get them on my drafts package but on the package my professor provided, I get these errors:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at gui.SnapShopGUI.createOpenButton(SnapShopGUI.java:188)
    at gui.SnapShopGUI.start(SnapShopGUI.java:133)
    at gui.SnapShopMain$1.run(SnapShopMain.java:36)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.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)

Image

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jasmine
  • 313
  • 2
  • 23
  • The image you are trying to load isn't where you think it is, it should be in the same package as the class which is trying to load it (base on your path) – MadProgrammer May 03 '16 at 00:38
  • @MadProgrammer I took those images out and now my code works, so thank you for that. The images I was trying to receive are in a folder outside the src folder, is there a different way from what I did to retrieve those images? – Jasmine May 03 '16 at 00:44
  • Just reference them as files `new ImageIcon(new File("{relative path}/open.gif"))`, just remember to use a relative path ;) (and before someone tells me I can just use a `String`, there's a reason why I'm not ;)) – MadProgrammer May 03 '16 at 00:46
  • Oh okay, so my project folder contains src, icons, and some other folders. Icons contains the images I want to access. Would the relative path just be "/icons/open.gif") then? I'm still kind of new with working with file paths, so I'm not sure. – Jasmine May 03 '16 at 00:58
  • It would be `icons/open.gif`, but that assumes that the program is executed on the parent directory of the `icons` directory ... confused yet ;) – MadProgrammer May 03 '16 at 01:02
  • I think I understand what you're saying. The program being executed is in a different folder than the icons folder. But they're in the same project folder. I'll edit my post with the image of my folder hierarchy so you can see. – Jasmine May 03 '16 at 01:07
  • 1
    *"I don't understand how the file works in one place but not the other."* See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson May 03 '16 at 02:46

0 Answers0