I wanted to get to know the GUI designer. For this I wanted to start out with a simple program that has a drop down menu with items in it. But for some reason the items are not displayed when executing.
package DSA;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class App {
public JPanel window_main;
public JComboBox<String> Klimaauswahl;
public App() {
Klimaauswahl.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
}
public static void main(String[] args) {
JFrame klima = new JFrame("Klimazone");
klima.setContentPane(new App().window_main);
klima.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
klima.pack();
klima.setVisible(true);
klima.setLocation(430, 100);
String[] klimazonen;
klimazonen = new String[]{ "Ewiges Eis", "Tundra", "Nördliche Steppen", "Nördliches
Hochland", "Kalkgebirge", "Mittelländische Gebirge", "Nördliche Wälder (Westküste)",
"Nördliche Wälder (Taiga)","Nördliche Wälder (Bornland)", "Nördliche Sümpfe",
"Mittelländisches Heideland", "Mittelländische Wälder", "Yaqirische Wälder", "Tobrische
Wälder", "Immergrüne Wälder", "Savanne", "Wüstenrandgebiete", "Wüste", "Südliche Gebirge",
"Maraskan", "Südliche Sümpfe", "Regenwald", "Regengebirge" };
final JComboBox<String> Klimaauswahl = new JComboBox<>(klimazonen);
klima.add(Klimaauswahl);
Klimaauswahl.setVisible(true);
}
}
The GUI form itself contains a JPanel named "window_main" and a JComboBox named "Klimaauswahl". Sorry, I used German words for my variables and Objects but to me and my buddy who work with the code it is easier to understand this way.
When I execute the program, there is no compiling error. But I get the following messages in the console:
Exception in thread "main" java.lang.NullPointerException
at m.intellij.uiDesigner.core.GridLayoutManager.addLayoutComponent(GridLayoutManager.java:134)
at java.awt.Container.addImpl(Container.java:1130)
at java.awt.Container.add(Container.java:1007)
at javax.swing.JFrame.addImpl(JFrame.java:567)
at java.awt.Container.add(Container.java:419)
at DSA.App.main(App.java:38)
In the end the program gets executed but the dropdown menu is empty. I really hope I provided all information.