Hello,

I would like to retieve data into my combobox.
I have done this so far but when I click on the button it's not giving me anything:

public void displayItem(int data1) {

        String sql = ("select * from Students limit ?,1");
        try {

            st = (PreparedStatement) con.prepareStatement(sql);
            st.setInt(1, data1);
            st.executeQuery();
            rs = st.getResultSet();

            while (rs.next()) {


                 String s = rs.getString("Nationality");
                    cb1.addItem(s.trim());

                //cb1.addItem(rs.getString("Nationality"));
                cb2.addItem(rs.getString("Duration"));
                cb3.addItem(rs.getString("Price"));
                cb4.addItem(rs.getString("Frequency"));
                cb5.addItem(rs.getString("Time"));
                cb6.addItem(rs.getString("Stage"));
                cb7.addItem(rs.getString("Teacher"));


            }


        } catch (Exception e) {
        }
    }

don't really see an action triggered by a (J)Button there. can you add a print in the catch statement? at this point, it is possible that you have a massive error message, but since you don't print it, you're just not aware of it.

Maybe there's an error when the code is run? But how would you know since you have told Java to discard any error messages and not to bother you with them?

How many times have we said this in the Java forum...
Never NEVER NEVER do this when writing new code:

} catch (Exception e) {
}

always put an e.printStackTrace(); in your catch blocks until/unless you have a good reason to do something else.

Edited 4 Years Ago by JamesCherrill

I have a null pointer exception.

The code for my button is:

 displayItem(a);
        a = a + 1;

and at the top I have declared a:

 int a = 1;

I don't understand why it is null, I have data in the database.

if you read the entire message, it will tell you exactly which variable on which line is null, that 'll make it easier to understand.

Maybe true, but on the other hand rs.getString does return null for some input values. I think it's better to encourage learners to adopt proper debugging techniques rather than starting with guesswork, so I'm not going to offer a guess on this one (oh, ok then, I put my money on s) ;)

In properties in the model section I wrote all the nationality( Afghan, Albanian, Algerian......) for the combobox .Howerver when I am clicking on the button it does retrieve the correct data. It always stays with Afgan.

Forgot to mention that cb1 is the variable name of the combobox

Edited 4 Years Ago by london-G

yes, we knew that. but that's not the issue. do you still have the NullPointerException, and, if so, on which variable, cb1 or s? both can be null if they're not instantiated or get that value passed.

When I printout cb1 I get this:
javax.swing.JComboBox[,90,310,193x26,layout=javax.swing.plaf.basic.BasicComboBoxUI$Handler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@1985910,flags=320,maximumSize=,minimumSize=,preferredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=Afghan]

When I printout s I get: 0

ehm... 0 is not the same as null.
if you do System.out.print(s);
and you get
0

then s = "0"; => not null
just add:
System.out.println("s == null ? " + (s == null) );
System.out.println("cb1 == null ? " + (cb1 == null) );

I get this:
cb1 == null ? false

For s, I get s underline as the variable is declare inside the try-catch

Where are you putting those prints? They should be immediately before the line that gave the NPE, ie

String s = rs.getString("Nationality");
// print values here
cb1.addItem(s.trim());

I fixed the NPE, now the problem I get is when I press the button, it is not showing the next reccord. The combobox value stays the same as the first reccord.

That's very interesting, but there's absolutely nothing anyone can do unless you post the code in question and an exact description of the roblem, incuding the complete text of any error messages or exceptions.

It's working now. The problem was due to me adding the string to the combobox but not displaying them when cliking on the button.
I added " cb1.setSelectedItem(s);"

Thanks a lot guys!

This question has already been answered. Start a new discussion instead.