0

When i try to add the calendar using JDatePicker, the selected calendar date is not printed.

i am in the confusion of whether the calendar date selected properly.when i select the date in the calendar i am getting the below errors.

Picked up JAVA_TOOL_OPTIONS: -agentlib:jvmhook Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at EDACheck.actionPerformed(StudentCheck.java:177) at org.jdatepicker.impl.JDatePanelImpl.fireActionPerformed(JDatePanelImpl.java:129) at org.jdatepicker.impl.JDatePanelImpl.access$1800(JDatePanelImpl.java:89) at org.jdatepicker.impl.JDatePanelImpl$InternalController.mousePressed(JDatePanelImpl.java:710) at java.awt.AWTEventMulticaster.mousePressed(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

The below is my code what i have tried,

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import org.jdatepicker.JDatePicker;
import org.jdatepicker.impl.JDatePanelImpl;
import org.jdatepicker.impl.JDatePanelImpl;
import org.jdatepicker.impl.JDatePickerImpl;
import org.jdatepicker.impl.UtilCalendarModel;
import org.jdatepicker.impl.UtilDateModel;

public class StudentCheck extends JPanel implements ActionListener {

    private JLabel jcomp1;
    private JTextField jcomp2;
    private JLabel jcomp3;
    private JTextField jcomp4;
    private JLabel jcomp5;
    private JTextField jcomp6;
    private JLabel jcomp7;
    private JTextField jcomp8;
    private JComboBox jcomp9;
    private JLabel jcomp10;
    private JButton jcomp11;
    private JLabel jcomp12;
    private JLabel jcomp13;
    private JTextField jcomp14;
    private JLabel jcomp15;
    private JLabel jcomp16;
    private JTextField jcomp17;
    private JTextField jcomp18;
    private JLabel jcomp19;
    private JTextField jcomp20;
    private JDatePickerImpl datePicker;

    public StudentCheck() {

        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        //tabbedPane.setBounds(10, 211, 733, 500);
        add(tabbedPane);

        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();

         tabbedPane.add("Student Check", panel1);

         tabbedPane.addTab("Student Check", null, panel1, null);

         tabbedPane.add("Teacher Check", panel2);

         tabbedPane.addTab("Teacher Check", null, panel2, null);
        //construct preComponents
         String[] jcomp9Items = {"maths", "science", "chemistry"};

         UtilDateModel model = new UtilDateModel();
        //model.setDate(20,04,2014);
        // Need this...
        Properties p = new Properties();
        p.put("text.today", "Today");
        p.put("text.month", "Month");
        p.put("text.year", "Year");
        JDatePanelImpl datePanel = new JDatePanelImpl(model, p);
        // Don't know about the formatter, but there it is...
        datePicker = new JDatePickerImpl(datePanel, new DateLabelFormatter());


        JButton buttonOK = new JButton("OK");


        //construct components
         jcomp1 = new JLabel ("Student Number");
         jcomp2 = new JTextField (5);
         jcomp3 = new JLabel ("Dep Date");
         //jcomp4 = new JTextField (5);
         jcomp5 = new JLabel ("  Dep Status");
         jcomp6 = new JTextField (5);
         jcomp7 = new JLabel ("          Options");
         jcomp8 = new JTextField (5);
         jcomp9 = new JComboBox (jcomp9Items);
         jcomp10 = new JLabel ("  platofrms");
         jcomp11 = new JButton ("Submit");
         jcomp12 = new JLabel ("                                                               Student and Teacher Availability Results in EDA");
         jcomp13 = new JLabel ("             blue flags");
         jcomp14 = new JTextField (5);
         jcomp15 = new JLabel ("    red flags");
         jcomp16 = new JLabel ("green flags");
         jcomp17 = new JTextField (5);
         jcomp18 = new JTextField (5);
         jcomp19 = new JLabel ("          Last  marks");
         jcomp20 = new JTextField (5);
         datePicker=new JDatePickerImpl(datePanel, null);
         panel1.add(datePicker);
         panel1.add(buttonOK);
         datePicker.addActionListener(this);

         buttonOK.addActionListener(this);


         jcomp11.addActionListener(this);
         jcomp9.addActionListener(this);

        //set components properties
        jcomp1.setToolTipText ("F");
        jcomp16.setToolTipText ("S");

        //adjust size and set layout
        panel1.setPreferredSize (new Dimension (733, 500));
        panel1.setLayout (null);

        //add components
        panel1.add (jcomp1);
        panel1.add (jcomp2);
        panel1.add (jcomp3);
        //panel1.add (jcomp4);
        panel1.add(datePicker);
        panel1.add (jcomp5);
        panel1.add (jcomp6);
        panel1.add (jcomp7);
        panel1.add (jcomp8);
        panel1.add (jcomp9);
        panel1.add (jcomp10);
        panel1.add (jcomp11);
        panel1.add (jcomp12);
        panel1.add (jcomp13);
        panel1.add (jcomp14);
        panel1.add (jcomp15);
        panel1.add (jcomp16);
        panel1.add (jcomp17);
        panel1.add (jcomp18);
        panel1.add (jcomp19);
        panel1.add (jcomp20);

        //set component bounds (only needed by Absolute Positioning)
        jcomp1.setBounds (50, 50, 100, 25);
        jcomp2.setBounds (180, 50, 100, 25);
        jcomp3.setBounds (340, 50, 100, 25);
        //jcomp4.setBounds (455, 50, 125, 25);
        jcomp5.setBounds (50, 95, 100, 25);
        jcomp6.setBounds (180, 100, 100, 25);
        jcomp7.setBounds (320, 100, 100, 25);
        jcomp8.setBounds (455, 95, 125, 25);
        jcomp9.setBounds (180, 160, 100, 25);
        jcomp10.setBounds (45, 160, 100, 25);
        jcomp11.setBounds (305, 210, 100, 25);
        jcomp12.setBounds (60, 250, 630, 25);
        jcomp13.setBounds (30, 290, 120, 25);
        jcomp14.setBounds (295, 290, 165, 25);
        jcomp15.setBounds (30, 335, 160, 25);
        jcomp16.setBounds (25, 370, 185, 30);
        jcomp17.setBounds (295, 330, 165, 25);
        jcomp18.setBounds (295, 370, 165, 25);
        jcomp19.setBounds (25, 410, 210, 25);
        jcomp20.setBounds (295, 405, 165, 25);
        datePicker.setBounds (455, 50, 125, 25);

    }
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

        // for UtilDateModel, the value returned is of type java.util.Date
        Date selectedDate = (Date) datePicker.getModel().getValue();
        System.out.println(selectedDate);

        // for UtilCalendarModel, the value returned is of type java.util.Calendar
//      Calendar selectedValue = (Calendar) datePicker.getModel().getValue();
//      Date selectedDate = selectedValue.getTime();

        // for SqlDateModel, the value returned is of type java.sql.Date
//      java.sql.Date selectedDate = (java.sql.Date) datePicker.getModel().getValue();

        JOptionPane.showMessageDialog(this, "The selected date is " + selectedDate);
    //some condtions to connect the database..

    //Getting the date from the caldenar in the format as YYYY-MM-DD and pass into the sql query.
}

    public static void main (String[] args) {
        JFrame frame = new JFrame ("MyPanel");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add (new StudentCheck());
        frame.pack();

        frame.setVisible (true);
    }


}

The same code will call for the date conversion for the selected date using the below class

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JFormattedTextField.AbstractFormatter;

public class DateLabelFormatter extends AbstractFormatter {

    private String datePattern = "yyyy-MM-dd";
    private SimpleDateFormat dateFormatter = new SimpleDateFormat(datePattern);

    @Override
    public Object stringToValue(String text) throws ParseException {
        return dateFormatter.parseObject(text);
    }

    @Override
    public String valueToString(Object value) throws ParseException {
        if (value != null) {
            Calendar cal = (Calendar) value;
            return dateFormatter.format(cal.getTime());
        }

        return "";
    }

}

When i click on the date its getting the above mentioned error,but the result expected is the selected date should be printed in the console and it should display in the popup too.

The below jars version i am using for this swing gui,

  • JdatePicker1.3.4.jar
  • xdb.jar
  • ojdbc.jar
  • xmlparserv2.jar
sathya
  • 199
  • 5
  • 26
  • Did you debug your code? Can you mark the line mentioned in the exception or yet better remove all the irrelevant clutter/code? Besides that, why are you reassigning `datePicker` ? – Thomas Jan 16 '17 at 16:18
  • Note that I guess you're talking about that line: `Date selectedDate = (Date) datePicker.getModel().getValue();` If so the only things that can cause a NullPointerException would be `datePicker` and the return value of `getModel()`. As such I'm inclined to close the question as a duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Thomas Jan 16 '17 at 16:25
  • Date selectedDate = (Date) datePicker.getModel().getValue(); yeah inam talking about the same line.here only I am getting the error.my question is why the date field not passed to the date picker and unable to select it. – sathya Jan 16 '17 at 17:36
  • Can you please point where I am re assigning the date picker ?@Thomas – sathya Jan 16 '17 at 17:41
  • 2
    You have those two lines: `JDatePickerImpl datePicker = new JDatePickerImpl(datePanel, new DateLabelFormatter());` and `datePicker=new JDatePickerImpl(datePanel, null);` - should be pretty obvious. – Thomas Jan 17 '17 at 08:26
  • 1
    "my question is why the date field not passed to the date picker and unable to select it" - you posted a stacktrace of a NullPointerException so that seems to be your problem. If there's anything else then post a [mcve] as well as what you tried to solve it (e.g. debugging etc.) and where you got stuck. – Thomas Jan 17 '17 at 08:42
  • yes i have removed the JDatePickerImpl in the code,its working fine @Thomas – sathya Jan 17 '17 at 11:41
  • 1
    Your output is correct (assuming you selected Nov 12th in the year 25 AD), the only problem is you get 316 as the day and I guess you want 12 instead. In that case have a look at the pattern string, especially the description of `DD` (and also `dd`). – Thomas Jan 17 '17 at 11:48
  • Sure I will check it @Thomas – sathya Jan 17 '17 at 16:25

0 Answers0