Martyn Martyn - 2 months ago 29
Java Question

NullPointerException with JDatePicker

I'm trying to replace a JTextField with a date picker field, I've followed the installation instructions I was able to find as well as other examples here on SO. Also, I have the latest version (v1.3.4) - https://sourceforge.net/projects/jdatepicker/

//...
import org.jdatepicker.impl.JDatePanelImpl;
import org.jdatepicker.impl.JDatePickerImpl;
import org.jdatepicker.impl.UtilDateModel;

public class NewTransactionDialog extends JDialog {

private JPanel panel = new JPanel(new GridLayout(0, 1));

//...

public NewTransactionDialog() {

//...

// date
panel.add(new JLabel("Date:"));
// panel.add(new JTextField()); // old text field i want to replace
UtilDateModel model = new UtilDateModel();
JDatePanelImpl datePanel = new JDatePanelImpl(model, null);
JDatePickerImpl datePicker = new JDatePickerImpl(datePanel, null);
panel.add(datePicker);

//...

getContentPane().add(panel);
pack();
}
}


Apologies if this is just some dumb error but I can't see from my side what I've done wrong this time. I did have to add the second NULL arguments coz Eclipse was telling me that the constructors didn't match. This seemed different from every example I say which only had the one arguments passed to the constructors (JDatePanelImpl, JDatePickerImpl) - should these be passed as something other than NULL?

The errors thrown in the eclipse console are:7

Exception in thread "main" java.lang.NullPointerException
at org.jdatepicker.impl.JDatePanelImpl$InternalView.getPreviousMonthButton(JDatePanelImpl.java:517)
at org.jdatepicker.impl.JDatePanelImpl$InternalView.getPreviousButtonPanel(JDatePanelImpl.java:442)
at org.jdatepicker.impl.JDatePanelImpl$InternalView.getNorthPanel(JDatePanelImpl.java:251)
at org.jdatepicker.impl.JDatePanelImpl$InternalView.initialise(JDatePanelImpl.java:234)
at org.jdatepicker.impl.JDatePanelImpl$InternalView.<init>(JDatePanelImpl.java:222)
at org.jdatepicker.impl.JDatePanelImpl.<init>(JDatePanelImpl.java:110)
at biz.martyn.budget.NewTransactionDialog.<init>(NewTransactionDialog.java:54)
at biz.martyn.budget.TransactionsToolbar.<init>(TransactionsToolbar.java:34)
at biz.martyn.budget.Budget.main(Budget.java:39)

Answer
at org.jdatepicker.impl.JDatePanelImpl$InternalView.getPreviousMonthButton(JDatePanelImpl.java:517)

That's where he fails. There is a method call on this.i18nStrings.getProperty("text.month")

but you are initializing JDatePanelImplwith null for that field:

  panel.add(new JLabel("Date:"));
  UtilDateModel model = new UtilDateModel();
  JDatePanelImpl datePanel = new JDatePanelImpl(model, null); // <<<<
  JDatePickerImpl datePicker = new JDatePickerImpl(datePanel, null); 
  panel.add(datePicker, BorderLayout.CENTER);

And thats a typical NullPointerException cause

The CTOR signature says: public JDatePanelImpl(DateModel<?> model, Properties i18nStrings) So it awaits a Properties object.