user7735600
user7735600

Reputation:

Unchecked cast warning in code

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.

Upvotes: 0

Views: 144

Answers (1)

M2E67
M2E67

Reputation: 970

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.

Upvotes: 1

Related Questions