Warning unchecked call

J

jc_usernet

Hello.
I am adding a simple method to a class for db access. It is to copy a
column of data from a ResultSet class to a Vector class object. It
works but I get a compile warning which I wish I knew how to remove.
The message is;
------------------------------------------------------------
ResultSetTableModel.java:175: warning: [unchecked] unchecked call to
add(E) as a member of the raw type java.util.Vector
v.add( resultSet.getInt(colName) );
------------------------------------------------------------

and the method I have added is

------------------------------------------------------------
// new method by JC
// obtain a column into a vector for a string column
public Vector getVectorFrmStringColumn( String colName )
throws IllegalStateException
{
Vector v = new Vector();
// ensure database connection is available
if ( !connectedToDatabase )
throw new IllegalStateException( "Not Connected to
Database" );

// obtain a string column specified by colName
try {
//System.out.println(" Before vector");
resultSet.beforeFirst();
while ( resultSet.next() ) {
v.add( resultSet.getString(colName) ); // this is line
175
}
}
catch( SQLException sqlException ) {
System.out.println( "sqlException occurred.");
sqlException.printStackTrace();
}
catch(Exception xception ) {
System.out.println(" The unknown exception ");
System.out.println( xception.toString() );
}
return v;
}
------------------------------------------------------------

How do I get rid of this warning ?
Is it the process of handling an unchecked exception ?

Regards JC.....
 
N

Nigel Wade

Hello.
I am adding a simple method to a class for db access. It is to copy a
column of data from a ResultSet class to a Vector class object. It
works but I get a compile warning which I wish I knew how to remove. The
message is;
------------------------------------------------------------
ResultSetTableModel.java:175: warning: [unchecked] unchecked call to
add(E) as a member of the raw type java.util.Vector
v.add( resultSet.getInt(colName) );
------------------------------------------------------------

The compiler is warning you that it cannot check that what you are adding
to the Vector is of the correct class. Presumably you have declared the
Vector without specifying what class it should contain.
and the method I have added is

------------------------------------------------------------ // new
method by JC
// obtain a column into a vector for a string column public Vector
getVectorFrmStringColumn( String colName )
throws IllegalStateException
{
Vector v = new Vector();

You have declared an untyped Vector. This Vector can contain anything,
the compiler is unable to verify that what you add to it is of the
correct type. So, the compiler will warn you every time you add something
to it that its type is unchecked.

// ensure database connection is available if ( !connectedToDatabase
)
throw new IllegalStateException( "Not Connected to
Database" );

// obtain a string column specified by colName try {
//System.out.println(" Before vector"); resultSet.beforeFirst();
while ( resultSet.next() ) {
v.add( resultSet.getString(colName) ); // this is line
175
}
}
catch( SQLException sqlException ) {
System.out.println( "sqlException occurred.");
sqlException.printStackTrace();
}
catch(Exception xception ) {
System.out.println(" The unknown exception ");
System.out.println( xception.toString() );
}
return v;
}

If the Vector is only supposed to contain objects of a particular type
then specify that in its declaration. In this case it looks like it only
holds Strings. If you declare it as:

Vector<String> v = new Vector<String>();

the compiler knows that your Vector should only contain Strings. It can
check this for you and the add() is no longer unchecked.

If the Vector really can contain objects of more than one type then you
can suppress the warning using @SuppressWarnings("unchecked") for that
method. But this is a blunt instrument and can mask problems which the
compiler would otherwise have told you about.
Is it the process of handling an unchecked exception ?

No, unchecked exceptions are entirely unrelated. They are a specific
subset of Exception which are not required to be caught, or declared as
being thrown, by a method.
 
L

Lew

Nigel said:
Hello.
I am adding a simple method to a class for db access. It is to copy a
column of data from a ResultSet class to a Vector class object. It
works but I get a compile warning which I wish I knew how to remove. The
message is;
------------------------------------------------------------
ResultSetTableModel.java:175: warning: [unchecked] unchecked call to
add(E) as a member of the raw type java.util.Vector
v.add( resultSet.getInt(colName) );
------------------------------------------------------------

The compiler is warning you that it cannot check that what you are adding
to the Vector is of the correct class. Presumably you have declared the
Vector without specifying what class it should contain.
and the method I have added is

------------------------------------------------------------ // new
method by JC
// obtain a column into a vector for a string column public Vector
getVectorFrmStringColumn( String colName )
throws IllegalStateException
{
Vector v = new Vector();

You have declared an untyped Vector. This Vector can contain anything,
the compiler is unable to verify that what you add to it is of the
correct type. So, the compiler will warn you every time you add something
to it that its type is unchecked.

// ensure database connection is available if ( !connectedToDatabase
)
throw new IllegalStateException( "Not Connected to
Database" );

// obtain a string column specified by colName try {
//System.out.println(" Before vector"); resultSet.beforeFirst();
while ( resultSet.next() ) {
v.add( resultSet.getString(colName) ); // this is line
175
}
}
catch( SQLException sqlException ) {
System.out.println( "sqlException occurred.");
sqlException.printStackTrace();
}
catch(Exception xception ) {
System.out.println(" The unknown exception ");
System.out.println( xception.toString() );
}
return v;
}

Learn about generics:
<http://java.sun.com/docs/books/effective/generics.pdf>

Don't use Vector. It's an old class that was superseded in 1998 by ArrayList.
 
M

markspace

Nigel said:
If the Vector really can contain objects of more than one type then you
can suppress the warning using @SuppressWarnings("unchecked") for that
method. But this is a blunt instrument and can mask problems which the
compiler would otherwise have told you about.


It is true that @SuppressWarnings is a blunt instrument. So if Vector
can contain any object, why not declare it as such?

Vector<Object> v = new Vector<Object>();
 
L

Lew

markspace said:
It is true that @SuppressWarnings is a blunt instrument. So if Vector
can contain any object, why not declare it as such?

Vector<Object> v = new Vector<Object>();

or, depending on whether you want a hetero- or homogenous List:

List <?> stuff = new ArrayList <?> ();

(Declare as the interface, implement as the concrete class. Don't use Vector.
Do use generics.)

The wildcard guarantees that all the elements have a common type that may be
lower than Object; Object as a type parameter doesn't guarantee that there's
any lower common supertype for all the elements.
 
M

markspace

Lew said:
List <?> stuff = new ArrayList <?> ();

(Declare as the interface, implement as the concrete class. Don't use
Vector. Do use generics.)


Those points above are good ones, I probably should have switched my own
example to something besides Vector.

The wildcard guarantees that all the elements have a common type that
may be lower than Object; Object as a type parameter doesn't guarantee
that there's any lower common supertype for all the elements.

However the problem with this is that you can't put anything into it,
not even Objects. For example, List<?> could hold a reference to
ArrayList<String>, so putting anything else, say a Number or even
Object, would be clearly wrong. Therefore the compiler prevents you
from doing so.

Wildcards aren't really for declaring variables, imo. They're for
enforcing constraints on parameter lists, and for type matching those
constraints inside such methods, which is where the confusion comes
from, I think. For example:

<T> List<T> copyList( List<T> list ) {
...
}

And not:

<T> List<? extends T> copyList( List<? extends T> list ) {
...
}

as the latter will be very difficult to use in practice. The first
declaration can be used as:

List<String> strlst = ...
List<String> strlst2 = copyList( strlst );

while the latter declaration forces:

List<? extends String> strlst2 = copyList( strlst );

which is going to be much harder to deal with due to the compiler type
constraints I mention above.

So in summary I don't think a wildcard type for the List (or Vector)
would be of much use to the OP, or anybody else really. Object is
really the only way to go if he's putting things into it of various
types. Of course, if he is using a single type like String, then it's
much better to declare the List (or Vector) with a parameter of the
narrower type.
 
L

Lew

However the problem with this is that you can't put anything into it,
not even Objects.  For example, List<?> could hold a reference to
ArrayList<String>, so putting anything else, say a Number or even
Object, would be clearly wrong.  Therefore the compiler prevents you
from doing so.

Wildcards aren't really for declaring variables, imo.  They're for
enforcing constraints on parameter lists, and for type matching those
constraints inside such methods, which is where the confusion comes
from, I think.  For example:

   <T> List<T> copyList( List<T> list ) {
     ...
   }

And not:

   <T> List<? extends T> copyList( List<? extends T> list ) {
     ...
   }

as the latter will be very difficult to use in practice.  The first
declaration can be used as:

   List<String> strlst = ...
   List<String> strlst2 = copyList( strlst );

while the latter declaration forces:

   List<? extends String> strlst2 = copyList( strlst );

which is going to be much harder to deal with due to the compiler type
constraints I mention above.

So in summary I don't think a wildcard type for the List (or Vector)
would be of much use to the OP, or anybody else really.  Object is
really the only way to go if he's putting things into it of various
types.  Of course, if he is using a single type like String, then it's
much better to declare the List (or Vector) with a parameter of the
narrower type.

Not only are your points correct, my suggestion won't even compile, as
the compiler rejects 'new ArrayList<?>()'.
 
R

Roedy Green

Joined
Apr 17, 2011
Messages
1
Reaction score
0
Vector vs. ArrayList

Came across this looking for a solution to the unchecked exception in Vector.

The question: In a multi-threaded server, the run() code reads a Vector of strings, and appends that to the current Vector of strings. Would like to know if there is a way to compile clean without the @SuppressWarnings("unchecked").

Code sample has been cut down to minimum problem:
Note: ois is the ObjectInputStream ( clientSocket.getInputStream() )

Have tried:
Vector<String> names = new Vector<String>(); // defined globally
names.add( (Vector<String>)ois.readObject() ); // in the run()
and other variants.

Any suggestions?

Would also like to make a comment on the statement to use ArrayList rather than Vector. I agree with Roedy that Vector is rarely used, however to clarify (not that it applies in the code example shown), ArrayList is not a replacement for Vector. Each has its purpose, and should not be thought of as 100% interchangeable.

ArrayList is more common and faster and I prefer using it, however, it is not thread safe. For example: a Vector is used when multiple threads could .add() or .remove() from the collection at the same time. Vectors ensure no loss of information. ArrayLists can be easily shown to lose added elements.

Rule of thumb: In all general coding ArrayList is OK, with multi-threaded applications use Vector.

PS: Roedy Green - Like your signature, funny and haven't heard much about FORTRAN in years.
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top