0

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
GalliadII
  • 162
  • 1
  • 14
  • See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Jul 19 '21 at 14:29

1 Answers1

1

You set content pane to null because window_main is never initialized before you use it.
There is also some confusion about which Klimaauswahl (please, don't start variable names with upper case) you want to use. There's one in App and there's another one, which you actually initialize in your main() which you add to frame.
I can only guess that you're somewhat confusing what is what here. Ask yourself what your App instance variables are and for what purpose you created them.

Ok, so here's some working example I made out of your code, I hope you read upon what I changed/added/removed and work it out from there.

package dsa;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class App {

    public static void main(String[] args) {

        JFrame klima = new JFrame("Klimazone");
        JPanel actualContentPane = new JPanel();
        final JLabel sampleLabel = new JLabel( "Initialized text" );
        String[] klimazonen = { "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);
        
        klimaauswahl.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) { 
                sampleLabel.setText( klimaauswahl.getSelectedItem().toString() );
            }
        });    
            
        klima.setContentPane( actualContentPane );
        klima.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        klima.setLocation(430, 100);
        klima.setSize( 300, 300 );

        klima.getContentPane().add(klimaauswahl);
        klima.getContentPane().add(sampleLabel);
        klima.setVisible(true);
    }
}
zubergu
  • 3,646
  • 3
  • 25
  • 38
  • thank you. I see the problem now. I needed to use the line: "JPanel actualContentPane = new JPanel();" And then I needed to use this JPanel to display everything. You really helped. :) – GalliadII Jun 26 '21 at 09:06
  • @GalliadII I'm glad to hear that. If I truly helped you please consider upvoting my answer and accepting it as a correct answer. That is the ultimate way to show you gratitude on StackOverflow. – zubergu Jun 26 '21 at 14:42