-2

I am a beginner at swing GUI and I am using a macbook pro and Eclipse Luna and my image is in the same folder as my code in finder, but in Eclipse the image doesn't show in the same folder as my code. See:

Image of the image in finder

Image of the image in eclipse

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;

class Frame extends JFrame {
    public Frame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Online First Game Image");

        //below is where one of the errors are

        ImageIcon image = new ImageIcon(this.getClass().getResource("/image.png"));
        JLabel label = new JLabel(image);
        JScrollPane scrollPane = new JScrollPane(label);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        add(scrollPane, BorderLayout.CENTER);
        pack();
   }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
           @Override
           public void run() {
              // where the other error is
              new Frame().setVisible(true);
           }
        });
    }
}

And Here Is The Error Message:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
    at Frame.<init>(Frame.java:13)
    at Frame$1.run(Frame.java:26)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

As I said, it would be SUPER helpful if someone can help me fix this bug.

Edit: I fixed the problem, and it had nothing to do with the code. It is not a duplicate and was a problem with trying to add an image to the Eclipse directory.

Ign1s Shotta
  • 87
  • 4
  • 14
  • I would strongly recommend putting the class in a package, and the image in a resource directory. – Andrew Thompson Dec 29 '17 at 02:55
  • @MLG HockeyPlayer: Hope this answer, [Loading Image as Resource](https://stackoverflow.com/a/9866659/1057230), might be of some help on the topic :-) – nIcE cOw Dec 30 '17 at 06:56

1 Answers1

0

Remove the slash before image.png:

Change this:

ImageIcon image = new ImageIcon(this.getClass().getResource("/image.png"));

by this:

ImageIcon image = new ImageIcon(this.getClass().getResource("image.png"));

Ele
  • 33,468
  • 7
  • 37
  • 75