0

Okay. I'm a beginner. I have looked at multiple questions on the interwebs, but I cannot fix this. I tried using URLs to show the image path, but nothing worked. Although, when I used ImageIcons to show the path, the picture just didn't show up; but when I used URLs, the JAR didn't open at at all. Then when I ran it in the command prompt, I got this--

Exception in thread "main" java.lang.NullPointerException
     at javax.swing.ImageIcon.<init>(Unknown Source)
     at Package.ListenerClass.<init>(ListenerClass.java:47)
     at Package.Class.main(Class.java:17)

What I'm getting out of this is that the file "doesn't exist". But when I compile it in eclipse, it works fine. Here's my driver class:

package Package;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Class {

@SuppressWarnings("unused")
public static void main(String args[]) {

        JFrame frame = new JFrame("Click If You Dare");
        ImageIcon background = new ImageIcon();
        JLabel label = new JLabel(background);

        ListenerClass panel = new ListenerClass();

        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        frame.pack();
        frame.setSize(425, 425);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setContentPane(panel);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            }

}

And my listener class:

package Package;

import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class ListenerClass extends JPanel implements MouseListener  {

ImageIcon buttonOriginal;
ImageIcon buttonClick;
ImageIcon button100;
ImageIcon button100Click;
ImageIcon button200;
ImageIcon button200Click;

JButton button;
JButton credits;
JButton reset;
JButton cheats;

JLabel secondLabel;

int clicks = 0;

@SuppressWarnings("static-access")
ListenerClass() {

    buttonOriginal = new ImageIcon(getClass().getResource("/button.png"));
    buttonClick = new ImageIcon(getClass().getResource("/buttonClick.png"));
    button100 = new ImageIcon(getClass().getResource("/button100.png"));
    button100Click = new ImageIcon(getClass().getResource("/button100Click.png"));
    button200 = new ImageIcon(getClass().getResource("/button200.png"));
    button200Click = new ImageIcon(getClass().getResource("/button200Click.png"));

    button = new JButton(buttonOriginal);
    button.setAlignmentX(button.CENTER_ALIGNMENT);

    button.setOpaque(false);
    button.setContentAreaFilled(false);
    button.setBorderPainted(false);
    button.setFocusPainted(false);

    credits = new JButton("Credits");
    credits.setAlignmentX(credits.CENTER_ALIGNMENT);

    reset = new JButton("Reset Score");
    reset.setAlignmentX(reset.CENTER_ALIGNMENT);

    secondLabel = new JLabel("" + clicks);
    secondLabel.setAlignmentX(secondLabel.CENTER_ALIGNMENT);
    secondLabel.setFont(new Font("DS-Digital", Font.PLAIN, 85) );

    button.addMouseListener(new MouseListener() {

        @Override
           public void mouseClicked(MouseEvent e) {
            clicks += 1;
            secondLabel.setText("" + clicks);

            if(clicks == 100) {
            button.setIcon(button100);
            }
            else if(clicks == 200) {
                button.setIcon(button200);
                }
           }

        @Override
        public void mouseEntered(MouseEvent arg0) {}
        @Override
        public void mouseExited(MouseEvent arg0) {}
        @Override
        public void mousePressed(MouseEvent arg0) {
            if(clicks <=99) {
                button.setIcon(buttonClick);
                }
                else if(clicks >= 100 && clicks <= 199) {
                button.setIcon(button100Click);
                }
                else if(clicks >= 200 && clicks <= 299) {
                    button.setIcon(button200Click);
                }
        }
        @Override
        public void mouseReleased(MouseEvent arg0) {
            if(clicks <=99) {
                button.setIcon(buttonOriginal);
                }
                else if(clicks >= 100 && clicks <= 199) {
                button.setIcon(button100);
                }
                else if(clicks >= 200 && clicks <= 299) {
                    button.setIcon(button200);
                }
        }

    });
    credits.addMouseListener(new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {
             JOptionPane.showMessageDialog(button, "Made by: Jonah Bobbitt and Wil Carpenter",
                                           null, JOptionPane.PLAIN_MESSAGE);
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {}
        @Override
        public void mouseExited(MouseEvent arg0) {}
        @Override
        public void mousePressed(MouseEvent arg0) {}
        @Override
        public void mouseReleased(MouseEvent arg0) {}

    });
    reset.addMouseListener(new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent e) {
            clicks = 0;
            secondLabel.setText("" + clicks);
            button.setIcon(buttonOriginal);
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {}
        @Override
        public void mouseExited(MouseEvent arg0) {}
        @Override
        public void mousePressed(MouseEvent arg0) {}
        @Override
        public void mouseReleased(MouseEvent arg0) {}

    });

    this.add(secondLabel);
    this.add(button);
    this.add(credits);
    this.add(Box.createVerticalStrut(10));
    this.add(reset);

    }
@Override
public void mouseClicked(MouseEvent arg0) {}
@Override
public void mouseEntered(MouseEvent arg0) {}
@Override
public void mouseExited(MouseEvent arg0) {}
@Override
public void mousePressed(MouseEvent arg0) {}
@Override
public void mouseReleased(MouseEvent arg0) {}
}

Help?

EDIT: Now when I use the URL, it no longer works when I compile it in eclipse, but it shows the same error. Also, the pictures are in a folder, which is in the build path.

EDIT...again: It's now working. See peeskillet's comment.

1 Answers1

0

Edit: If the image is within your package you can load the resource using only the name of the file (without the leading slash)

buttonOriginal = new ImageIcon(getClass().getResource("button.png"));

Though it is better practice to separate the resource in a separate folder.

enrique7mc
  • 897
  • 9
  • 13
  • I would down-vote but it's Christmas :-) Have you tried this? URL is the right way – Paul Samsotha Dec 24 '14 at 16:21
  • Yes, I've tried this myself, but an aclaration about why it's better to use URL would be appreciated. – enrique7mc Dec 24 '14 at 16:24
  • Because reading from the file system won't work this way once the images are in a jar. String argument to ImageIcon constructor is to read from the file system from working dir. Please try it with a runnable jar. – Paul Samsotha Dec 24 '14 at 16:25
  • I tried this, but this is what I was talking about. When I do this, the ImageIcon does not show up when exported. – ThePurplePixel Dec 24 '14 at 16:39