线程中的例外... Jcombobox,Jbutton和ActionListener
我正在研究一个学校项目,我想通过按钮从文本字段中添加文本到组合框。我保存对象
及其属性,然后将其添加到组合框中:在我的情况下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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您发布的堆栈跟踪说它在第119行,尽管我不确定那是哪一行。我将整个代码粘贴到本地编辑器中,只看到117行。
堆栈跟踪确实表明它是由
ActionPerformed()
抛出的,这是您的代码。从那里开始,我们可以推断以下代码块中至少有以下内容之一是null
- “至少一个”,因为脚步上可能有多个null 等待您发现:
txtkategorien
kategorienliste
cbbkateaus
neuekategorie
使用A调试器是最好的,但是在
ActionPerformed()
方法的开始时添加这些行将帮助您确定问题: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.
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 isnull
– "at least one" because, pedantically, there might be more than onenull
waiting for you to discover:txtKategorien
kategorienListe
cbbkateaus
neueKategorie
Using a debugger would be best, but adding these lines at the start of your
actionPerformed()
method would help you identify the problem: