Java [unchecked] unchecked case warning
•
Java
Well, I've been looking around and doing a lot of Google searches, but I still can't find a way to avoid this warning
Integer result = chooser.showOpenDialog(null); if (result.equals(0)) { String tempHolder = chooser.getSelectedFile().getPath(); filenameLoad = new File(tempHolder); filenameSave = filenameLoad; FileInputStream fis = null; ObjectInputStream in = null; try { fis = new FileInputStream(filenameLoad); in = new ObjectInputStream(fis);; } catch(IOException ex) { ex.printStackTrace(); } try { loadFile = (ArrayList<Dot>)in.readObject(); } catch(IOException ex) { System.out.println("Cast fail"); } catch(ClassNotFoundException ex) { System.out.println("Cast fail"); } catch (ClassCastException ex) { System.out.println("Cast fail"); } try { in.close(); } catch(Exception ex) { System.out.println("Failed to close in"); } save.setEnabled(true); gpanel.setDotList(loadFile); }
It is in line LoadFile = (ArrayList) in Warning is given in readobject(); I've added the catch, so I'm not sure why it still says it's not captured Does it help? thank you?
Solution
It is not "not captured", but "unchecked" The JVM cannot tell at runtime whether the ArrayList really contains a dot element when the conversion is completed
This warning occurs when converting from a primitive type to a generic type If you are sure that the cast is OK, you can use comments to suppress warnings
@SuppressWarnings("unchecked")
For this purpose, it is best to package the casting in a small and separate method
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码