0

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)
Martyn
  • 6,031
  • 12
  • 55
  • 121
  • No these are classes of a JAR library I imported belonging to the JDatePicker libary. I can see the method calls in the error message and those aren't my methods either, so it must surely be accessing the library classes. I'll update my post to show how I'm importing these classes. I installed the library after downloading the JAR file via Eclipses library install wizard. – Martyn Sep 23 '16 at 21:33

2 Answers2

1

You need to pass the second parameter in the constructor as an instance of Properties. It could be simply:

JDatePanelImpl datePanel = new JDatePanelImpl(model, new Properties());
JDatePickerImpl datePicker = new JDatePickerImpl(datePanel, new Properties());

This will get rid of the NullPointerException, but you might see empty strings. Read the documentation for the library. I looked at the source and it seems the Properties instance should have these properties defined: text.today, text.month, text.year.

Balkrishna Rawool
  • 1,865
  • 3
  • 21
  • 35
1
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.

xetra11
  • 7,671
  • 14
  • 84
  • 159
  • 1
    Thanks. Found a solution here too - http://stackoverflow.com/questions/26794698/how-do-i-implement-jdatepicker – Martyn Sep 23 '16 at 23:38