0

My code:

 private List<Day> readDays(File file) {
        List<Day> days = new ArrayList<>();
        try {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
            days.addAll((List<Day>) in.readObject());
        } catch (IOException | ClassNotFoundException e) {
            Logger.logError(LOG_TAG, e);
        }
        return days;
    }

Unchecked cast problem in this code

 days.addAll((List<Day>) in.readObject());

And this is a problem, in some cases the app crashes.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • 4
    Possible duplicate of [How do I address unchecked cast warnings?](https://stackoverflow.com/questions/509076/how-do-i-address-unchecked-cast-warnings) – germi Aug 08 '17 at 10:07
  • I cant comment on your question so... On a related post someone found that as to be the solution: https://stackoverflow.com/a/509230/5554959 Maybe it could help. – EAzevedo Aug 08 '17 at 10:11

1 Answers1

1

if your problem is cast object; you can define a convertor to convert your object to your class and handle exceptions.

if your stream return json string, You can use ObejctMapper and convert json string to your class as follow method by using jackson library:

    //create ObjectMapper instance
    ObjectMapper objectMapper = new ObjectMapper();

    //convert json string to object
    Day day = objectMapper.readValue(jsonData, Day.class); 

   // use day class now

so converting object, depend on your file data format.

M2E67
  • 937
  • 7
  • 23