0

While inserting the selected item from JComboBox it show's an Null Pointer Exception how to fix this?

This is for an java swing Registration form using java eclipse and Mysql

if    (    gender.getSelectedIndex()!=-1)
                {
                     //g is ComboBox variable name
                String    value    =    g.getSelectedItem().toString();
                sta.setString(4, value);
                }

i except the selected item from combobox to be stored in mysql

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

2

You can simply check for null before inserting it like that

if(value != null)
  sta.setString(4, value);

You can also consider this

String value = String.valueOf(g.getSelectedItem());
AhmedR
  • 76
  • 3