Java [unchecked] 未检查的情况警告
好吧,我环顾四周并做了很多谷歌搜索,但我仍然找不到避免此警告的方法。
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);
}
它在 loadFile = (ArrayList)in.readObject(); 行给了我警告我已经添加了捕获,所以我不确定为什么它仍然说未捕获。有什么帮助吗?谢谢?
Ok, I've been looking around and done alot of google searching, 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 gives me the warning at the line loadFile = (ArrayList)in.readObject(); I've added in the catchs so i'm not sure why it still says its uncatched. Any help? thanks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它不是“不受控制”,而是“不受控制”。 JVM 无法在运行时(即转换完成时)判断 ArrayList 是否确实包含 Dot 元素。
每当您从原始类型转换为泛型类型时,都会出现此警告。如果您确定强制转换正确,则可以使用注释抑制警告。
为此,最好将强制转换封装在一个小的、单独的方法中。
It is not "uncatched", but "unchecked". The JVM cannot tell at runtime, i.e. when the cast is done, whether the ArrayList really contains Dot elements.
This warning occurs whenever you cast from a raw type to a generic type. If you are sure the cast is ok, you can suppress the warning with annotation
For this, it is good to encapsulate the cast in a small, separate method.
您没有检查返回的对象是否
真的是
Use
You are not checking whether the object returned by
is really an
Use
它并没有说
uncatched
(正确拼写为uncaught
),而是说unchecked
。当转换为泛型类型时,您无法避免此警告,只能抑制它。或者您可以解决它:使用此方法您可以编写:
It doesn't say
uncatched
(which correctly is spelleduncaught
), butunchecked
. You cannot avoid this warning when casting to a generic type, you can only suppress it. Or you can work around it:With this method you can write:
这意味着编译器无法检查您读入的对象是否与您将其转换为的类型匹配。因此出现了未经检查的警告。
顺便说一句:您可能希望改进错误处理以使其更简单、更清晰。 ;)
It means the compiler cannot check that the object you read in, matches the type you are casting it to. Thus the unchecked warning.
BTW: You might want to work on your error handling to make it simpler and clearer. ;)
您看到的是编译器警告,您正在尝试将对象转换为
ArrayList
,而无需先检查该对象是否实际上包含 Dot 列表,而不是 Foo 列表。What you see is a compiler warning that you are trying to convert an Object into a
ArrayList<Dot>
without first checking if the Object actually contains a List of Dot and not e.g. a List of Foo.