Error: class java.lang.String cannot be cast to class java.lang.Number

Victor Butler6 years ago

Hello Anton,

I just stumbled on one strange error with traccar server today. When trying to get a Report on the Trips for the day for a teltonika device I get the following error:

class java.lang.String cannot be cast to class java.lang.Number (java.lang.String and java.lang.Number are in module java.base of loader 'bootstrap') - ClassCastException (ExtendedModel:97 < ReportUtils:98 < MotionEventHandler:89 < ...)

I also get a similar error when I try to get the Summary:
class java.lang.String cannot be cast to class java.lang.Number (java.lang.String and java.lang.Number are in module java.base of loader 'bootstrap') - ClassCastException (ExtendedModel:97 < ReportUtils:98 < Summary:65 < ...)

The reports for Route, Events, Stops and Chart work normally.

Do you know what could be the reason for this error? Let me know if you need further information!

Anton Tananaev6 years ago

You have odometer value as a string. I suspect that you have an incorrect computed attribute.

Victor Butler6 years ago

Indeed you are correct. The Odometer was set a String instead of Number.

I have fixed the computed attribute and restarted the Traccar server (to clear the cache) but I still get the same error. Do I need to fix anything else so that I can fetch the report?

Thank you!

Anton Tananaev6 years ago

Changing attribute doesn't change the history. It will only affect new data.

Victor Butler6 years ago

This is true for computed attributes. The odometer data recorded in the database has been parsed as string at the time of the record itself.

However, the data itself seems to be correct, i.e. odometer value increasing in the positions table and there is no value (as far as I can tell) which tells the server that odometer should be string or number. So it is either the odometer values in the DB that are incorrect or the server still considers those past values as strings.

If my reasoning is correct, which of the two causes the error as of now?

Victor Butler6 years ago

I found the problem. When values are reported as strings they are encapsed with double quotes like so "odometer":"50000" while if they are set as numbers they are recorded without the double quotes like so "odometer":50000.
So because of the strings being recorded with " the error appeared.