2017-12-28 29 views
-2

Ich bin ein Anfänger bei Swing GUI und ich benutze ein Macbook Pro und Eclipse Luna und mein Bild ist im selben Ordner wie mein Code im Finder, aber in Eclipse der Das Bild wird nicht im selben Ordner wie mein Code angezeigt. Siehe:java.lang.NullPointerException Swing GUI auf getResource für eine ImageIcon

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); 
      } 
     }); 
    } 
} 

und hier ist die Fehlermeldung:

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) 

Wie gesagt, es wäre SUPER hilfreich sein, wenn jemand kann mir diesen Fehler helfen beheben .

Edit: Ich habe das Problem behoben, und es hatte nichts mit dem Code zu tun. Es ist kein Duplikat und es ist ein Problem beim Versuch, ein Bild zum Eclipse-Verzeichnis hinzuzufügen.

+0

ich stron würde Gly empfiehlt, die Klasse in ein Paket und das Bild in ein Ressourcenverzeichnis einzufügen. –

+0

@MLG Hockeyspieler: Hoffe, diese Antwort, [Lade Bild als Ressource] (https://stackoverflow.com/a/9866659/1057230), könnte etwas Hilfe zum Thema sein :-) –

Antwort

0

Entfernen Sie den Schrägstrich vor image.png:

ändern diese:

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

durch diese:

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

+0

Da die Klasse bereits in der leeres Paket, das macht keinen Unterschied. – VGR

+0

Ich habe schon das Problem behoben, das mit dem Code nichts zu tun hatte –