I seem to be having some sort of an issue suppressing a warning that I get from deserializing an ArrayList containing type MyObject, from file. The error message I am getting is from the line
objects = (ArrayList<MyObject>) ois.readObject();
It reads:
Type safety: Unchecked cast from Object to ArrayList<MyObject>
After I apply the @SuppressWarnings(“unchecked”) to the specific warning, it says that my objects object cannot be resolved to a type.
ArrayList<MyObject> objects;
try {
FileInputStream fis = new FileInputStream(new File("myfile.txt"));
ObjectInputStream ois = new ObjectInputStream(fis);
@SuppressWarnings("unchecked")
objects = (ArrayList<MyObject>) ois.readObject(); //Getting 'objects cannot be resolved to a type'
fis.close();
}
catch (Exception e) {
objects = new ArrayList<MyObject>();
}
This is very strange because if I apply @SuppressWarnings(“unchecked”) to the entire method, the error goes away. Also, it IS occurring on multiple machines, not just on one setup.
Does anyone have any insight as to what is going on that makes this error appear when I suppress only the one instance of the error?
@SupressWarnings("unchecked")can only be applied to the declariation of the object, this will work:… but may not solve your problem. The reason for this can be found in the JDK source code. The
@Targetannotation on the@SupressWarningsannotation defines the permitted locations: