0

I'm trying to convert sql.date which i'm getting from my sqlite database to util.date. And i want to set that date to JDateChooser. I've literally tried all possible ways but it seems i'm missing something. Can anybody tell me what's wrong?

try{
    String query = "select * from SkaterInfo where reg_no = ?";
    pst = con.prepareStatement(query);
    pst.setInt(1, num1);
    rs = pst.executeQuery();

    while(rs.next()){
         name = rs.getString("Name");
         surname = rs.getString("Surname");
         batch = rs.getInt("Batch");
         cell = rs.getLong("Mobile_No");
         date = rs.getDate("Birth_Date");
    }


    pst.close();
    rs.close();

}catch(Exception a){
    JOptionPane.showMessageDialog(null, "Exception");
}

java.sql.Date date1 = new java.sql.Date(date.getTime());
dateChooser.setBounds(150, 150, 150, 30);
dateChooser.setDateFormatString("yyyy-MM-dd");
dateChooser.setDate(date1);
frame.getContentPane().add(dateChooser);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Parag Pawar
  • 827
  • 3
  • 12
  • 23
  • 1
    Do you get any error messages? – emanciperingsivraren Jul 04 '16 at 15:57
  • yeah,, kind of.. actually not errors,, lots of exceptions..:p – Parag Pawar Jul 04 '16 at 16:00
  • Include the exceptions in your post, to give us a hint. They should come from the code you posted. – emanciperingsivraren Jul 04 '16 at 16:03
  • to be more specific it is giving me NullPointerException. – Parag Pawar Jul 04 '16 at 16:03
  • 1
    Then I would say you get the nullpointer because you don't have a date. Add a check before to see if your java.sql.date is null or not. (If it is null you can not convert it...) – emanciperingsivraren Jul 04 '16 at 16:05
  • I'm sorry but i'm new to java.. :| so can you tell me how to check it? should i try if else? n yes then where. – Parag Pawar Jul 04 '16 at 16:15
  • And bdw how could it be null if I'm accessing it from my database? – Parag Pawar Jul 04 '16 at 16:18
  • Because it is `null` in the database, or because there are no records with that `reg_no` in the database. – Andreas Jul 04 '16 at 16:30
  • Question: Given that `rs.getDate()` returns a `java.sql.Date` object for the `date` variable, why are you converting that `java.sql.Date` to another `java.sql.Date` object for the `date1` variable, when your title says you want a `java.util.Date` object? BTW: Conversion is unnecessary, because a `java.sql.Date` object *is* a `java.util.Date` object. – Andreas Jul 04 '16 at 16:33
  • yeah right, i should convert it to java.util.Date. So i tried that too. But still not working. And I've checked the database too, it has Date entry. And if conversion is not necessary, it should be easy to set date by just passing sql date to JDateChooser, but it's not working either – Parag Pawar Jul 04 '16 at 16:43
  • If possible just tell me how should i set date retrieved from database to JDateChooser. – Parag Pawar Jul 04 '16 at 17:03
  • @ParagPawar "It's not working" "I have a NullPointerException" is not really helpful. Unless you post a stacktrace, which points to a specific line of code, it's going to be nothing more than guessing. A good first step would be to avoid "swallowing" exceptions like you do in the catch block. Print the stack trace instead, it will give you a lot of valuable information. And read the first few answers of the duplicate to understand how to debug a NullPointerException. – assylias Jul 04 '16 at 17:10
  • As i said earlier, i'm still new to java and i'm still learning. But thanks :) I'll try to improve.. :) – Parag Pawar Jul 04 '16 at 17:27

0 Answers0