-1

I am working on a school project and I want to add text from a text field through a button to a combo box. I save the Object with its attributes and then I add it to the combo box: in my case cbbkateaus.

When I run the app it doesn't add it to the combo box and I get this error

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at MtVwGui$2.actionPerformed(MtVwGui.java:119)

Followed by further javax.swing... errors but I think the former is the relevant one. I tried it with the String that I have in // but it leads to the same result. (names of objects might be in German as an FYI)

import java.awt.BorderLayout;
public class MtVwGui extends JFrame {

    private JPanel contentPane;
    private JTextField txtHersteller;
    private JTextField txtModell;
    private JTextField txtBaujahr;
    private JTextField txtHubraum;
    private JTextField txtKategorien;
    private ArrayList<Kategorien> kategorienListe;
    private ArrayList<Motorraeder> motorraederListe;
    private Kategorien neueKategorie;
    private Motorraeder neuesMotorrad;
    private JComboBox cbbkateaus;
    

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MtVwGui frame = new MtVwGui();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MtVwGui() {
        kategorienListe= new ArrayList<Kategorien>();
        motorraederListe= new ArrayList<Motorraeder>();
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 888, 501);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        JLabel lbHersteller = new JLabel("Hersteller:");
        lbHersteller.setBounds(21, 95, 46, 14);
        contentPane.add(lbHersteller);
        
        txtHersteller = new JTextField();
        txtHersteller.setBounds(101, 92, 86, 20);
        contentPane.add(txtHersteller);
        txtHersteller.setColumns(10);
        
        JLabel lbModell = new JLabel("Modell:");
        lbModell.setBounds(21, 128, 46, 14);
        contentPane.add(lbModell);
        
        txtModell = new JTextField();
        txtModell.setBounds(101, 123, 86, 20);
        contentPane.add(txtModell);
        txtModell.setColumns(10);
        
        JLabel lbBaujahr = new JLabel("Baujahr:");
        lbBaujahr.setBounds(21, 153, 46, 14);
        contentPane.add(lbBaujahr);
        
        txtBaujahr = new JTextField();
        txtBaujahr.setBounds(101, 154, 86, 20);
        contentPane.add(txtBaujahr);
        txtBaujahr.setColumns(10);
        
        JLabel lblNewLabel = new JLabel("Hubraum:");
        lblNewLabel.setBounds(21, 190, 54, 14);
        contentPane.add(lblNewLabel);
        
        txtHubraum = new JTextField();
        txtHubraum.setBounds(101, 187, 86, 20);
        contentPane.add(txtHubraum);
        txtHubraum.setColumns(10);
        
        JButton btnaddMtr = new JButton("Motorrad hinzuf\u00FCgen");
        btnaddMtr.setBounds(21, 259, 138, 23);
        contentPane.add(btnaddMtr);
        
        txtKategorien = new JTextField();
        txtKategorien.setBounds(446, 92, 100, 20);
        contentPane.add(txtKategorien);
        txtKategorien.setColumns(10);
        
        JButton btnaddKate = new JButton("Kategorie hinzuf\u00FCgen");
        btnaddKate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                
                neueKategorie = new Kategorien(txtKategorien.getText());
                
                //String addValue = txtKategorien.getText();
                kategorienListe.add(neueKategorie);

                cbbkateaus.addItem(neueKategorie);
                
                System.out.println("add Kategorie wurde gedrückt");
                System.out.println(neueKategorie.getBezeichnung());
                
            }
        });
        btnaddKate.setBounds(594, 91, 138, 23);
        contentPane.add(btnaddKate);
        
        JComboBox cbbkateaus = new JComboBox();
        
        cbbkateaus.setModel(new DefaultComboBoxModel(new String[] {"none"}));
        cbbkateaus.setBounds(74, 215, 130, 23);
        contentPane.add(cbbkateaus);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
kdendon
  • 1
  • 2
  • 2
    [mcve] please .. mind the __M__ (nothing unrelated) and include the complete stacktrace formatted as code – kleopatra Jun 30 '22 at 22:40

1 Answers1

0

The stack trace you posted says it's on line 119, though I'm not sure which line that is. I pasted the entire code into a local editor and see only 117 lines.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at MtVwGui$2.actionPerformed(MtVwGui.java:119)

The stack trace does show that it was thrown by actionPerformed(), which is your code. From there, we can infer that at least one of the following things in the code block below is null – "at least one" because, pedantically, there might be more than one null waiting for you to discover:

  • txtKategorien
  • kategorienListe
  • cbbkateaus
  • neueKategorie
btnaddKate.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        
        neueKategorie = new Kategorien(txtKategorien.getText());
        
        //String addValue = txtKategorien.getText();
        kategorienListe.add(neueKategorie);

        cbbkateaus.addItem(neueKategorie);
        
        System.out.println("add Kategorie wurde gedrückt");
        System.out.println(neueKategorie.getBezeichnung());
        
    }
});

Using a debugger would be best, but adding these lines at the start of your actionPerformed() method would help you identify the problem:

System.out.println("txtKategorien: " + txtKategorien);
System.out.println("kategorienListe: " + kategorienListe);
System.out.println("cbbkateaus: " + cbbkateaus);
System.out.println("neueKategorie: " + neueKategorie);
Kaan
  • 5,434
  • 3
  • 19
  • 41