Statisches JLabel adden

jalako

Mitglied
Hallo liebe Community,
ich habe folgendes Problem:

Ich erstelle in der Klasse Fight.java ein static JLabel namens "test".
Dieses möchte ich in zu einem JFrame, welches sich in einer void in einer anderen Klasse befindet adden.

Die Klasse mit dem JLabel:
Java:
public class Fight {

    public static JLabel test;
    public Fight()
    {
            Music.playIntro();
      
            Icon icon = new ImageIcon("res/fightintro.gif");
            JLabel test = new JLabel(icon);
    }

}

Die Klasse mit dem JFrame:
Java:
public void Gameframe(){
  
    Game game = new Game();
  
    JFrame frame = new JFrame("Pokemon");
    Inventory inven = new Inventory();
    frame.add(Inventory.inv2);
    frame.add(Fight.test);
    frame.add(game);
  
    frame.pack();
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    game.start();
    }

}

Die JLabel inv2 und game, die ich ebenfalls adde, funktionieren. Wenn ich jedoch das JLabel test adde, kommt folgender Fehler:

Java:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at javax.swing.JFrame.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at pokemon.Menu.Gameframe(Menu.java:354)
    at pokemon.Menu$1.mousePressed(Menu.java:119)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(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.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.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)

Vielen Dank im Vorraus für eure Hilfe!
 
Zuletzt bearbeitet von einem Moderator:
Die statische Variable test wird nirgendwo initialisiert. Du initialisierst stattdessen eine gleichnamige lokale Variable im KOnstruktor von Fight.
Das Design mit einem public und obendrein staric JLabel in einer nicht-Gui-Klasse sieht übrigens "daneben" aus.
 
Zuletzt bearbeitet:
Okay danke schon mal! Hab das mit dem JLabel geändert...
So ist es richtig oder?:
Java:
public class Fight {
    public static JLabel test;
    public Fight ()
    {
            Music.playIntro();
          
            test = new JLabel(new ImageIcon(getClass().getResource("/resource/fightintro.gif")));
            test.setBounds(440,0,650, 400);
            test.setVisible(true);  
    }
}

Gleicher Fehlercode.... :/
 
Zuletzt bearbeitet von einem Moderator:
Dann wird test immer noch nicht initialisiert. Das ist dann z. B. der Fall, wenn der Konstruktor von Fight nie aufgerufen wird.
In der aktuellen Form macht dein Code keine Sinn. Was soll ein statisches JLabel in einer nicht-Gui-Klasse? Warum initialisierst du das JLabel nicht in der Klasse, zu der die GameFrame-Methode gehört?
 
Die Frage von Harry Kane ist berechtig, aber unabhängig davon:
Die Variable public static JLabel test ist beim Aufruf (also an der Stelle frame.add(Fight.test)😉 noch nicht initiallisiert. Dies machst du in deinem Projekt ja erst im Konstruktor der Klasse Fight. Da du die Klasse Fight aber niergends instanziiert hast, ist die Varaible test natürlich noch null an der Stelle.
 

Zurück
Oben