Calendar : NPE if some date is null

Hello all,

I came into some NullPointerException using calendar Add-on (7.2.1)
After some digging i found out that Calendar does not like so much having some date with null value, especially start-date or end-date… ok quite normal :slight_smile:

Stack trace :
java.lang.NullPointerException
at java.util.Calendar.setTime(Calendar.java:1106)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:955)
at java.text.SimpleDateFormat.format(SimpleDateFormat.java:948)
at java.text.DateFormat.format(DateFormat.java:336)
at com.vaadin.ui.Calendar.setupCalendarEvents(Calendar.java:435)
at com.vaadin.ui.Calendar.beforeClientResponse(Calendar.java:307)

In case of using some DBContainer (JPAContainer, SQLContainer, …), it’s not so easy to control and correct values.
Shouldn’t it be a good idea to make some checks and make some simple logic : for instance if no end-date is set, put same as start-date. No start-date then skip…

Or may be there is some trick to implement those rules by myself ? Any suggestion ?

Best regards
Sebastien

Hi Sebastien,

At least you can do something like the below

private Calendar getCustomCalendar()  {
        return new Calendar() {
            @Override
            public void setStartDate(Date date) {
                if (date!=null) {
                    super.setStartDate(date);
                } else {
                    super.setStartDate(new Date());
                }
            }

            @Override
            public void setEndDate(Date date) {
                if (date!=null && date.compareTo(startDate)>=0) {
                    super.setEndDate(date);
                } else if (startDate!=null) {
                    super.setEndDate(startDate);
                } else {
                    super.setEndDate(new Date());
                }
            }
            
            @Override
            public void beforeClientResponse(boolean initial) {
                if (startDate==null) {
                    endDate = new Date();
                }
                if (endDate==null) {
                    endDate = new Date(startDate.getTime());
                }
                super.beforeClientResponse(initial);
            }
            
        };
    }

Nice !
Exactly what I need.

Many thanks
Sébastien

Hello all,

Unfortunately it’s not working. “setStartDate” and “setEndDate” functions are to set the calendar boundaries, not the tasks’ dates.

I opened a ticket to try to resolve this as I made multiple tries, but all without success.

http://dev.vaadin.com/ticket/14130

Regards
Sebastien