2

私が取り組んでいるこのアプリには 3 つのラジオ ボタンがありますが、そのうちの 1 つを選択し、他の 2 つを選択せず​​に JFrame を開く必要があります。

JFrame の読み込み時に、次のメソッドを呼び出します。

private void initJRadio() {
    jRadioButton1.setSelected(false);
    jRadioButton2.setSelected(true);
    jRadioButton3.setSelected(false);
}

しかし、JFrame のロード時に次の例外が発生します。

スレッド「AWT-EventQueue-0」での例外 java.lang.NullPointerException at StockJFrame.initJRadio(StockJFrame.java:139)

ここで、StockJFrame はクラス名、139 は「jRadioButton1.setSelected(false);」の行番号です。

このクラスの [ソース] ペインに、Netbeans によって次の行が追加されました。

jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();

jRadioButton1.setText(/*label value*/);
jRadioButton1.setToolTipText(/*some tooltip text*/);
jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        jRadioButton1ActionPerformed(evt);
    }
});

jRadioButton2.setText(/*label value*/);
jRadioButton2.setToolTipText(/*some tooltiptext*/);
jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        jRadioButton2ActionPerformed(evt);
    }
});

jRadioButton3.setText(/*label value*/);
jRadioButton3.setToolTipText(/*some tooltip text*/);
jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        jRadioButton3ActionPerformed(evt);
    }
});

これを正しく設定するには?

4

1 に答える 1