Question

I am getting ClassCastException while getting the value from the Model.Please find the exception details below:

Caused by: java.lang.ClassCastException: java.util.Date cannot be cast to java.lang.String
at com.csscorp.presentation.spinneret.client.ams.AttendanceCaseCreationModel.getCheckinDate (AttendanceCaseCreationModel.java:164)
 at com.csscorp.presentation.spinneret.client.ams.AttendanceCaseCreationGridEditor$8.handleEvent   (AttendanceCaseCreationGridEditor.java:916)

Below is my code:

             ColumnConfig checkinDatecolumn = new ColumnConfig();
     checkinDatecolumn.setId("checkinDate");
     checkinDatecolumn.setHeader("Check In Date");
     checkinDatecolumn.setWidth(85);
     checkinDatecolumn.setMenuDisabled(true);
     checkinDatecolumn.setSortable(false);
    final DateField dateField1 = new DateField();
    dateField1.getPropertyEditor().setFormat(DateTimeFormat.getFormat("dd/MMM/yyyy")); 
     final String tempDate=dateField1.getRawValue();

    dateField1.getDatePicker().addListener(Events.Select, new Listener<DatePickerEvent>() {

        @Override
        public void handleEvent(DatePickerEvent dpe) {

        if(ACCCheckBoxModel.getSelectedItem().getRosterDate()!=null){
        DateTimeFormat format = DateTimeFormat.getFormat("dd/MMM/yyyy");
        rosterdate=format.parse(ACCCheckBoxModel.getSelectedItem().getRosterDate());
            nextdate.setTime(rosterdate.getTime()+(1000*60*60*24));
            prevdate.setTime(rosterdate.getTime()-(1000*60*60*24));
        }
        int rowIndex = caseStoreModule.indexOf(ACCCheckBoxModel.getSelectedItem());
        Window.alert("Row Index-->"+rowIndex);



    if(ACCCheckBoxModel.getSelectedItem().getCheckinDate().toString() == null){//here I  am getting ClassCastException
                                            if (rosterdate.compareTo(dateField1.getValue())==0 || nextdate.compareTo(dateField1.getValue())==0){
                Window.alert("this is a valid date-->");
            }

            else {
                Window.alert("Enter a valid date-->");
                dateField1.reset();
            }



        }

        else{
//Here I need to Implement validation logic if the value from the model is not null

        }
            } 
    }); 

In model class I am setting the checkinDate as String and DTO also I am setting it as String only. But Why ClassCastException is coming. I am not getting the cause for this.

Update

This is the line of AttendanceCaseCreationModel.java:164

163   public String getCheckinDate() {
164     

return get("checkinDate");

165 }

Update

After calling toString method,ClassCastException is not coming.

return get("checkinDate").toString();

But When I check for null value,It's throwing the NullPointerException.

if(ACCCheckBoxModel.getSelectedItem().getCheckinDate() == null)

Exception details are below:

Caused by: java.lang.NullPointerException: null at com.csscorp.presentation.spinneret.client.ams.AttendanceCaseCreationModel.getCheckinDate(AttendanceCaseCreationModel.java:167) at com.csscorp.presentation.spinneret.client.ams.AttendanceCaseCreationGridEditor$8.handleEvent(AttendanceCaseCreationGridEditor.java:920)

Was it helpful?

Solution 2

I solved this.By below code change.

public String getCheckinDate() {

    if(get("checkinDate")==null)
            return null;
        else 
            return get("checkinDate").toString();


            }

Now it returns a String if it is not null.And null if it is null.

OTHER TIPS

Try the following:

public String getCheckinDate() 
{
   final Date date = (Date) get( "CheckinDate" );
   return date == null ? null : date.toString();
}

Remove the toString() from getCheckinDate().toString().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top