java Swing 单选按钮 - java.lang.NullPointerException [英] java swing radio buttons - java.lang.NullPointerException
问题描述
我正在尝试使用 java swing 并测试单选按钮.我的代码是:
I'm trying to get to grips with java swing and was testing out radio buttons. My code is:
import java.awt.*;
import javax.swing.*;
import javax.swing.ButtonGroup;
public class Scafhome extends javax.swing.JFrame {
private JRadioButton bandButton;
private JRadioButton gelButton;
private JButton jbtnRun;
public Scafhome() {
JFrame jfrm = new JFrame("Scaffold search ...");
jfrm.setLayout (new GridLayout(8,2));
jfrm.setSize(320,220);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JRadioButton bandButton = new JRadioButton();
bandButton.setText("Band-id");
bandButton.setSelected(true);
JRadioButton gelButton = new JRadioButton();
gelButton.setText("Gelc-ms");
ButtonGroup group = new ButtonGroup();
group.add(bandButton);
group.add(gelButton);
JButton jbtnRun = new JButton("RUN");
jbtnRun.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
RunActionPerformed(evt);
}
});
jfrm.add(bandButton);
jfrm.add(gelButton);
jfrm.add(jbtnRun);
jfrm.setVisible(true);
}
private void RunActionPerformed(java.awt.event.ActionEvent evt) {
String radioText="";
if (bandButton.isSelected()) {
radioText=bandButton.getText();
}
if (gelButton.isSelected()) {
radioText=gelButton.getText();
}
javax.swing.JOptionPane.showMessageDialog( Scafhome.this, radioText );
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Scafhome();
}
});
}
}
不幸的是,我收到以下错误消息:
Unfortunately I get the following error message:
线程AWT-EventQueue-0"中的异常 java.lang.NullPointerException在 Scafhome.RunActionPerformed(Scafhome.java:50)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Scafhome.RunActionPerformed(Scafhome.java:50)
这是在:"if (bandButton.isSelected()) {"
This is at: "if (bandButton.isSelected()) {"
我认为bandButton"已被创建并标记为已选择" - 还是我误解了什么?
I thought 'bandButton' had been created and marked as 'selected' - or have I misunderstood something?
非常感谢,卷曲.
推荐答案
您正在隐藏 bandButton 变量——您是在构造函数中重新声明它并初始化本地重新声明的变量,而不是离开类的类字段字段为空.解决方案——不要重新声明变量.
You're shadowing the bandButton variable -- you're re-declaring it in the constructor and initializing the local redeclared variable, not the class field leaving the class field null. Solution -- don't re-declare the variable.
为了明确,改变这个:
public class Scafhome extends javax.swing.JFrame {
private JRadioButton bandButton;
//...
public Scafhome() {
//...
// re-declared variable!
JRadioButton bandButton = new JRadioButton();
为此:
public class Scafhome extends javax.swing.JFrame {
private JRadioButton bandButton;
//...
public Scafhome() {
//...
// variable not re-declared
bandButton = new JRadioButton();
请注意,您正在为类中声明的所有三个变量执行此操作.
Note that you're doing this for all three variables that you've declared in the class.
这篇关于java Swing 单选按钮 - java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!