线程中的例外... Jcombobox,Jbutton和ActionListener

发布于 2025-02-12 09:21:04 字数 4439 浏览 1 评论 0原文

我正在研究一个学校项目,我想通过按钮从文本字段中添加文本到组合框。我保存对象及其属性,然后将其添加到组合框中:在我的情况下cbbkateaus

当我运行该应用程序时,它不会将其添加到组合框中,我会得到此错误

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

,然后再将其添加到其他javax.swing ...错误,但我认为前者是相关的。我使用字符串//中尝试过,但它会导致相同的结果。 (物体的名称可能是德语作为仅供参考的)

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过期情话 2025-02-19 09:21:04

您发布的堆栈跟踪说它在第119行,尽管我不确定那是哪一行。我将整个代码粘贴到本地编辑器中,只看到117行。

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

堆栈跟踪确实表明它是由ActionPerformed()抛出的,这是您的代码。从那里开始,我们可以推断以下代码块中至少有以下内容之一是null - “至少一个”,因为脚步上可能有多个null 等待您发现:

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

使用A调试器是最好的,但是在ActionPerformed()方法的开始时添加这些行将帮助您确定问题:

System.out.println("txtKategorien: " + txtKategorien);
System.out.println("kategorienListe: " + kategorienListe);
System.out.println("cbbkateaus: " + cbbkateaus);
System.out.println("neueKategorie: " + neueKategorie);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文