How can I iterate through JComboBox and Validate items
<p>i am trying to iterate through JComboBox items i.e its connected to Database, whenever I click it it fetches data from Database and updates it. But its adding duplicate values in such case. I am trying to validate it by iterating through each item once added to JComboBox, if the existing item is similar to item I am trying to add then it shall not add and jump to other statement. </p>

<p>However I am then getting Null pointer error, in order to avoid this error first time I added <code>counter -1</code>, but once items are added and  want to update, it gives error.</p>

<p>My JComboBox code is given below:</p>

<pre><code>comboBox.addMouseListener(
            new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent ev) {
                    List&lt;Guest&gt; list = null;
                    list = database.readGuest();
                    int n = list.size();

                    if(n &lt;= 0) {
                        JOptionPane.showMessageDialog(null, "No data found.");
                    }else {
                        for(int count = 0; count &lt; n; count++) {
                            g = list.get(count);
                            String pass = g.getPassportNp();
                            //String s = (String) comboBox.getItemAt(count-1);
                            //for(int i = 0; i &lt; n; ++i) {
                                if(comboBox.getItemCount() != 0) {
                                    if(comboBox.getItemAt(count-1).equals(pass)) {
                                        continue;
                                    }else {
                                        comboBox.addItem(pass);
                                    }
                                }else {
                                    comboBox.addItem(pass);
                                }
                            //}

                        }
                    }
                }
            });
</code></pre>

<p>any solution for this?</p>