3

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?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
convergedtarkus
  • 697
  • 2
  • 9
  • 19

5 Answers5

10

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.

Ingo
  • 36,037
  • 5
  • 53
  • 100
2

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;
}
Buurman
  • 1,914
  • 17
  • 26
  • 2
    Eclipse says: Cannot perform instanceof check against parameterized type ArrayList. Use the form ArrayList> instead since further generic type information will be erased at runtime. – cdunn2001 Feb 10 '12 at 22:04
1

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());
Roland Illig
  • 40,703
  • 10
  • 88
  • 121
0

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

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Yea, I just learned about exceptions like 4 days ago, so when I got that warning I though it meant I needed to catch an exception, so I just hit it with everything. – convergedtarkus Apr 13 '11 at 01:14
  • @marblecatdog, Sounds fine for now. You might find that haven't a small number of catches around all the code is better. – Peter Lawrey Apr 13 '11 at 08:31
0

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.

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119