I’m getting a “Type safety: Unchecked cast from Object to ArrayList” warning on the line with readObject(), in this code snippet:
// Read the Event List
theEventArrayList = new ArrayList<Event>();
String FILENAME = "EventData.dat";
FileInputStream fis;
try {
fis = openFileInput(FILENAME);
ObjectInputStream ois = new ObjectInputStream(fis);
theEventArrayList = (ArrayList<Event>) ois.readObject();
fis.close();
}
Event is a simple class comprised of some Strings, Calendars, booleans, and ints. The ArrayList is written using an ObjectOutputStream in a mirror image operation to the above. The application this code is used in is executed many times a day for over a month with no failures, but the compiler warning bothers me and I don’t want to just suppress it if it can be “checked” properly.
Suppress it. Your only other alternative is to cast to ArrayList, but then everywhere else in your code you have to deal with untyped ArrayList and casting on read. There is no harm in suppressing in this situation.