-2

Java.lang.NullPointerException というエラーが発生しています。Java の初心者です。これについて助けてください。Netbeans でこのコードを実行しています。

if ((cmbDate.getSelectedItem().equals("")) &&  (cmbMonth.getSelectedItem().equals("")) && (cmbYear.getSelectedItem().equals(""))) {
           JOptionPane.showMessageDialog( this, "Please select DOB","Error", JOptionPane.ERROR_MESSAGE);
           return;
            }
4

3 に答える 3

1

when you have a static content and a dynamic variable to test against using eqauls, always use the static part in the left like

"".equals(cmbDate.getSelectedItem())

it will rescue from cases where cmbDate.getSelectedItem() is null.

Also assuming the objects also can be null

if ((cmbDate == null || "".equals(cmbDate.getSelectedItem())) ||  (cmbMonth ==null || "".equals(cmbMonth.getSelectedItem())) || (cmbYear ==null ||"".equals(cmbYear.getSelectedItem()))) {
    JOptionPane.showMessageDialog( this, "Please select DOB","Error", JOptionPane.ERROR_MESSAGE);
    return;
}
于 2013-08-27T04:05:00.413 に答える
0

Please ensure any of this controls return null item and verify the control itself.

 cmbDate.getSelectedItem() 
 cmbYear.getSelectedItem()
 cmbYear.getSelectedItem()

And change the code like below. If you got any null we are verified but your desire ouput will vary.

     if ((cmbDate.getSelectedItem() !=null && cmbDate.getSelectedItem().equals(""))                       
              &&  (cmbMonth.getSelectedItem() !=null &&cmbMonth.getSelectedItem().equals(""))
          && (cmbYear.getSelectedItem() !=null && cmbYear.getSelectedItem().equals(""))) {
               JOptionPane.showMessageDialog( this, "Please select DOB","Error",    JOptionPane.ERROR_MESSAGE);
             return;
       }
于 2013-08-27T04:05:15.563 に答える
0
  1. cmbDate
  2. cmb月
  3. cmbYear

上記のいずれもヌルにすることができます。操作を実行する前に、null かどうかを確認する必要があります。

于 2013-08-27T04:07:31.447 に答える