Ng Wei Ze
Ng Wei Ze

Reputation: 1

I can't select the value in Jcombo box

I am currently doing a project on ordering system. I have problem with getting the values of the Jcombo Box. Here's the code for Jcombo Box:

JComboBox cb_tableno = new JComboBox();
cb_tableno.setBounds(424, 250, 93, 20);
 cb_tableno.setModel(new DefaultComboBoxModel(new String[]{ "1","2","3","4","5","6","7","8","9","10"}));
 add(cb_tableno);

private void actionPerformedOrder() { //retrieve user input

    String tableNo= (String)cb_tableno.getSelectedItem();
   Date orderDate = new Date();
   orders = new Orders(Integer.parseInt(tableNo),orderDate, totalAmount);
   int orderID = OrdersDA.createOrders(orders);

    }

There is a Jbutton which is called "create" . When i have selected the value in my Jcombo box and press create, there is run time error String tableNo= (String)cb_tableno.getSelectedItem();: and null pointer exception.

Upvotes: 0

Views: 57

Answers (1)

Paco Abato
Paco Abato

Reputation: 4065

Maybe the line:

JComboBox cb_tableno = new JComboBox();

Is hidding the cb_tableno object that you acces in:

String tableNo= (String)cb_tableno.getSelectedItem();

And that is why the NullPointerException occurs.

Upvotes: 2

Related Questions