Java [unchecked] 未检查的情况警告

发布于 2024-11-01 04:52:46 字数 1138 浏览 0 评论 0原文

好吧,我环顾四周并做了很多谷歌搜索,但我仍然找不到避免此警告的方法。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鸵鸟症 2024-11-08 04:52:46

它不是“不受控制”,而是“不受控制”。 JVM 无法在运行时(即转换完成时)判断 ArrayList 是否确实包含 Dot 元素。

每当您从原始类型转换为泛型类型时,都会出现此警告。如果您确定强制转换正确,则可以使用注释抑制警告。

@SuppressWarnings("unchecked")

为此,最好将强制转换封装在一个小的、单独的方法中。

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

@SuppressWarnings("unchecked")

For this, it is good to encapsulate the cast in a small, separate method.

温馨耳语 2024-11-08 04:52:46

您没有检查返回的对象是否

in.readObject();

真的是

ArrayList<Dot>

Use

ArrayList<Dot> dotList = null;
Object obj = in.readObject();
if (obj instanceof ArrayList<Dot>)
{
dotList = (ArrayList<Dot>) obj;
}

You are not checking whether the object returned by

in.readObject();

is really an

ArrayList<Dot>

Use

ArrayList<Dot> dotList = null;
Object obj = in.readObject();
if (obj instanceof ArrayList<Dot>)
{
dotList = (ArrayList<Dot>) obj;
}
伤感在游骋 2024-11-08 04:52:46

它并没有说uncatched(正确拼写为uncaught),而是说unchecked。当转换为泛型类型时,您无法避免此警告,只能抑制它。或者您可以解决它:

@SuppressWarnings("unchecked")
public static <T> T castToAnything(Object obj) {
  return (T) obj;
}

使用此方法您可以编写:

loadFile = castToAnything(in.readObject());

It doesn't say uncatched (which correctly is spelled uncaught), but unchecked. You cannot avoid this warning when casting to a generic type, you can only suppress it. Or you can work around it:

@SuppressWarnings("unchecked")
public static <T> T castToAnything(Object obj) {
  return (T) obj;
}

With this method you can write:

loadFile = castToAnything(in.readObject());
新一帅帅 2024-11-08 04:52:46

这意味着编译器无法检查您读入的对象是否与您将其转换为的类型匹配。因此出现了未经检查的警告。

顺便说一句:您可能希望改进错误处理以使其更简单、更清晰。 ;)

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. ;)

时光暖心i 2024-11-08 04:52:46

您看到的是编译器警告,您正在尝试将对象转换为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文