Unchecked cast warning?

K

Knute Johnson

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,
 
T

Thomas Hawtin

Knute said:
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
 
S

su_dang

Thomas said:
Knute said:
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.
 
K

Knute Johnson

Thomas said:
Knute said:
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.
 
K

Knute Johnson

Thomas said:
Knute said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top