1

I have a table with a column datetime i want to retrieve data from database where date is specified in jdatechooser but em continously getting error:

Cannot make a static reference to the non-static method getDate() from the type JDateChooser

Here is the code:

public void actionPerformed(ActionEvent e) {

                Date date = JDateChooser.getDate();
                try{ 
                String query = " Select *from Transactions WHERE "+date+"=?  ";
                PreparedStatement pst = con.prepareStatement(query);
                ResultSet rs = pst.executeQuery();
                table.setModel(DbUtils.resultSetToTableModel(rs));
                }catch (Exception e1){
                    e1.printStackTrace();
                }

            }
Amna Doll
  • 19
  • 2
  • Possible duplicate of [Cannot make a static reference to the non-static method](https://stackoverflow.com/questions/4969171/cannot-make-a-static-reference-to-the-non-static-method) – Oleg Sep 19 '17 at 04:59
  • 1
    Unrelated, but: that's the wrong way to use a `PreparedStatement`. The query should be `Select *from Transactions WHERE date_column = ?` then use `pst.setDate(date)` to pass the selected value. –  Sep 19 '17 at 07:03

1 Answers1

1

It's theoretically possible to have a window with lots of different JDateChooser controls on it. So when you refer to one of them, you need to specify which one, rather than just calling it JDateChooser.

Somewhere in your class, you'll have a declaration something like

private JDateChooser theChooser;

where you declare a variable to refer to your JDateChooser - that is, you give it a name. Now, you need to use the EXACT SAME NAME when you refer to your JDateChooser in your actionPerformed method. For example

Date date = theChooser.getDate();  

But don't write theChooser - write whatever name YOU gave the JDateChooser when you declared the variable.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • I changed as you said to this: Date date = jDate.getDate(); but now getting NULL POINTER EXCEPTION ERROR ON THE SAME LINE – Amna Doll Sep 19 '17 at 05:18