-1

I'm trying to pass the data from the JTable for which all the types are String then I want to pass to the JDateChooser when the user selected the row in the table. But I tried parsing the date from string to date type but still, I got an error saying illegal value.

Here is the code.

private void tableDataMouseClicked(java.awt.event.MouseEvent evt) {
    try{
        DefaultTableModel model = (DefaultTableModel) tableData.getModel();
        int selectedRowIndex = tableData.getSelectedRow(); //get selected row
        Fname.setText(model.getValueAt(selectedRowIndex,1).toString());
        Lname.setText(model.getValueAt(selectedRowIndex,2).toString());
        ageSpin.setValue(model.getValueAt(selectedRowIndex,3).toString());
        Date date = new SimpleDateFormat("yyyy-MM-dd").parse((String)model.getValueAt(selectedRowIndex, 4).toString());
        dob.setDate(date);
        addressField.setText(model.getValueAt(selectedRowIndex,5).toString());
        phoneNumField.setText(model.getValueAt(selectedRowIndex,6).toString());
        emailField.setText(model.getValueAt(selectedRowIndex,7).toString());
    }catch(ParseException e){
        e.printStackTrace();
        Logger.getLogger(addCitizzen.class.getName()).log(Level.SEVERE, null, e);
    }
}

Here is the error I'm getting:

java.lang.IllegalArgumentException: illegal value

The program gives an error when it reaches the Date part.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 3
    A better choice would be to allow the model to support a `Date` object – MadProgrammer Nov 24 '21 at 08:45
  • [Edit] your question and post the entire [stack trace](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors). – Abra Nov 24 '21 at 11:56
  • Please provide enough code so others can better understand or reproduce the problem. – Community Nov 28 '21 at 15:56

1 Answers1

0

The error is arising due to argument passed. Here is the working one:

Date date = new SimpleDateFormat("dd-MM-yyyy").parse((String)model.getValueAt(selectedRowIndex, 4).toString());
Dimitris Maragkos
  • 8,932
  • 2
  • 8
  • 26
Hima
  • 1
  • This looks exactly like the line stated as the error causing line in the original. Where is the difference? And what was the problem that you fixed? – Pao Sep 20 '22 at 09:19