1

I'm trying to pull in a date so I can work with it in SQL. I put the jDateChooser on the screen and use it to define a string. Because it starts out with no value (null), it throws Null Pointer Exception. I initialize the JDateChooser with compdate.setCalendar(Calendar.getInstance()); This sets the date to today and that is the value I get returned when I pull in the string. The code is below and I would love to resolve this. I'm guessing it is from my ignorance about when the update should be firing...

    JDateChooser compdate = new JDateChooser();
    compdate.setDateFormatString("yyyy/MM/dd");
    compdate.setBounds(26, 75, 144, 23);
    compdate.setCalendar(Calendar.getInstance());
    String jcalval = (new java.text.SimpleDateFormat("yyyy/MM/dd")).format(compdate.getDate());
    panelReporting.add(compdate);
    System.out.println(jcalval);
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
David S.
  • 45
  • 1
  • 10
  • You can add a listener like [this](https://stackoverflow.com/a/4157956/261156). – Catalina Island Feb 14 '18 at 18:39
  • Thanks so much Greg. I got it to update the date, but when I pull the date using the jcalval statement, it shows today's date. Not sure where to go from here. The statement is after the update, but then again, I don't really know the order it is processing the update. – David S. Feb 14 '18 at 20:28
  • And I believe it's resolved as I just had to move the updated string = to another event. IT's working now, thanks so much guys!! – David S. Feb 14 '18 at 20:45
  • If this is not a duplicate, please [edit] your question to include a [mcve] that shows your revised approach. – trashgod Feb 15 '18 at 21:47

1 Answers1

1

The OP reported that he fixed the issue by adding a listener as suggested in the comments and the linked question:

    compdate.getDateEditor().addPropertyChangeListener(new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent e) {

             if ("date".equals(e.getPropertyName())) 
                {
                    System.out.println(e.getPropertyName()
                        + ": " + (Date) e.getNewValue());
                }
            }
        });        

(This was posted in the question, where it doesn’t belong; I just took it out into an answer.)

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161