2

I was trying to use a combobox to show contents in a table. I wrote the code below, bu nothing displayed in the table.

Integer i = ((Destination) (jComboBox1.getSelectedItem())).getId();
query1 = entityManager.createQuery("SELECT d FROM Dayactivity d WHERE d.id=:Id");
query1.setParameter("Id", i);
java.util.Collection data = query1.getResultList();
list2.clear();
list2.addAll(data);

When I changed the code as below it returned data correctly and displayed in the table.

Integer i = ((Destination) (jComboBox1.getSelectedItem())).getId();
query1 = entityManager.createQuery("SELECT d FROM Dayactivity d WHERE d.id=:Id");
query1.setParameter("Id", 2);
java.util.Collection data = query1.getResultList();
list2.clear();
list2.addAll(data);

Why It is not working for the first code (i) but works for 2 in second code?

Can somebody help me to solve this, I am new to Java & NetBeans

Sarath Babu
  • 923
  • 3
  • 10
  • 27

2 Answers2

0

Change how i is initialized to:

int i = jComboBox1.getSelectedIndex();

Starkey
  • 9,673
  • 6
  • 31
  • 51
consprice
  • 722
  • 3
  • 11
  • 21
0

Do one of the following:

  • Debug the code to see the real value of i:

if i is null then you need to change how you access the selected id of combobox as follows:

Integer i = ((Destination) jComboBox1.getSelectedIndex();
  • You need to check the argument of his method:

    1. query1.setParameter(String, int); or
    2. query1.setParameter(String, Integer);
GingerHead
  • 8,130
  • 15
  • 59
  • 93