Unchecked cast warning?

Discussion in 'Java' started by Knute Johnson, Nov 1, 2006.

  1. I'm trying to read a Hashtable from an ObjectInputStream and getting an
    Unchecked cast warning in the compiler.

    Hashtable<Integer,String[]> table =
    (Hashtable<Integer,String[]>)ois.readObject();

    com\knutejohnson\redrock\scores\ScoresServer.java:209: warning:
    [unchecked] unch
    ecked cast
    found : java.lang.Object
    required: java.util.Hashtable<java.lang.Integer,java.lang.String[]>
    (Hashtable<Integer,String[]>)ois.readObject();
    ^
    1 warning

    Where am I going wrong here?

    Thanks,

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Nov 1, 2006
    #1
    1. Advertising

  2. Knute Johnson wrote:
    > I'm trying to read a Hashtable from an ObjectInputStream and getting an
    > Unchecked cast warning in the compiler.
    >
    > Hashtable<Integer,String[]> table =
    > (Hashtable<Integer,String[]>)ois.readObject();


    This seems to be a common problem...

    http://groups.google.com/group/comp...98473ba/5d022881ad375d93#doc_06f9e33d0ffd3e41


    /**
    * @throws ClassCastException
    * @see ObjectInputStream#readObject
    */
    @SuppressWarnings("unchecked")
    static <T> readObject(
    ObjectInputStream in
    ) throws IOException, ClassNotFoundException {
    return (T)in.readObject();
    }

    Tom Hawtin
     
    Thomas Hawtin, Nov 1, 2006
    #2
    1. Advertising

  3. Knute Johnson

    Guest

    Thomas Hawtin wrote:
    > Knute Johnson wrote:
    > > I'm trying to read a Hashtable from an ObjectInputStream and getting an
    > > Unchecked cast warning in the compiler.
    > >
    > > Hashtable<Integer,String[]> table =
    > > (Hashtable<Integer,String[]>)ois.readObject();

    >
    > This seems to be a common problem...
    >
    > http://groups.google.com/group/comp...98473ba/5d022881ad375d93#doc_06f9e33d0ffd3e41
    >
    >
    > /**
    > * @throws ClassCastException
    > * @see ObjectInputStream#readObject
    > */
    > @SuppressWarnings("unchecked")
    > static <T> readObject(
    > ObjectInputStream in
    > ) throws IOException, ClassNotFoundException {
    > return (T)in.readObject();
    > }
    >
    > Tom Hawtin


    There was no way for the compiler to make sure that the object type is
    correct; hence, it issues the warning. As long as you know what you
    are doing, it should be ok.
     
    , Nov 1, 2006
    #3
  4. Thomas Hawtin wrote:
    > Knute Johnson wrote:
    >> I'm trying to read a Hashtable from an ObjectInputStream and getting
    >> an Unchecked cast warning in the compiler.
    >>
    >> Hashtable<Integer,String[]> table =
    >> (Hashtable<Integer,String[]>)ois.readObject();

    >
    > This seems to be a common problem...
    >
    > http://groups.google.com/group/comp...98473ba/5d022881ad375d93#doc_06f9e33d0ffd3e41
    >
    >
    >
    > /**
    > * @throws ClassCastException
    > * @see ObjectInputStream#readObject
    > */
    > @SuppressWarnings("unchecked")
    > static <T> readObject(
    > ObjectInputStream in
    > ) throws IOException, ClassNotFoundException {
    > return (T)in.readObject();
    > }
    >
    > Tom Hawtin


    Thanks Tom.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Nov 1, 2006
    #4
  5. wrote:
    > Thomas Hawtin wrote:
    >> Knute Johnson wrote:
    >>> I'm trying to read a Hashtable from an ObjectInputStream and getting an
    >>> Unchecked cast warning in the compiler.
    >>>
    >>> Hashtable<Integer,String[]> table =
    >>> (Hashtable<Integer,String[]>)ois.readObject();

    >> This seems to be a common problem...
    >>
    >> http://groups.google.com/group/comp...98473ba/5d022881ad375d93#doc_06f9e33d0ffd3e41
    >>
    >>
    >> /**
    >> * @throws ClassCastException
    >> * @see ObjectInputStream#readObject
    >> */
    >> @SuppressWarnings("unchecked")
    >> static <T> readObject(
    >> ObjectInputStream in
    >> ) throws IOException, ClassNotFoundException {
    >> return (T)in.readObject();
    >> }
    >>
    >> Tom Hawtin

    >
    > There was no way for the compiler to make sure that the object type is
    > correct; hence, it issues the warning. As long as you know what you
    > are doing, it should be ok.
    >


    Thanks.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Nov 1, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Lee
    Replies:
    2
    Views:
    3,126
    Andrew Lee
    Nov 18, 2004
  2. Ian Pilcher

    Avoid unchecked cast warning

    Ian Pilcher, Aug 20, 2005, in forum: Java
    Replies:
    0
    Views:
    3,541
    Ian Pilcher
    Aug 20, 2005
  3. Knute Johnson
    Replies:
    2
    Views:
    2,069
    Knute Johnson
    Apr 15, 2007
  4. kathychow
    Replies:
    0
    Views:
    2,123
    kathychow
    Mar 31, 2009
  5. RVic
    Replies:
    19
    Views:
    1,398
Loading...

Share This Page