Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Warning unchecked call

Reply
Thread Tools

Warning unchecked call

 
 
jc_usernet
Guest
Posts: n/a
 
      01-22-2010
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.....
 
Reply With Quote
 
 
 
 
Nigel Wade
Guest
Posts: n/a
 
      01-22-2010
On Fri, 22 Jan 2010 02:31:43 -0800, jc_usernet wrote:

> 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;
> }
> ------------------------------------------------------------
>
> How do I get rid of this warning ?


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.

--
Nigel Wade

 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      01-22-2010
Nigel Wade wrote:
> On Fri, 22 Jan 2010 02:31:43 -0800, jc_usernet wrote:
>
>> 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;
>> }
>> ------------------------------------------------------------
>>
>> How do I get rid of this warning ?


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.

--
Lew
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      01-22-2010
Nigel Wade wrote:

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


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      01-22-2010
markspace wrote:
> Nigel Wade wrote:
>
>> 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>();


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.

--
Lew
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      01-22-2010
Lew wrote:

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






 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      01-22-2010
Lew wrote:
>> *List <?> stuff = new ArrayList <?> ();

^^ compile error
>> (Declare as the interface, implement as the concrete class. *Don't use
>> Vector. *Do use generics.)

>


markspace wrote:
> 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<?>()'.

--
Lew
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-23-2010
On Fri, 22 Jan 2010 02:31:43 -0800 (PST), jc_usernet
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>{
> Vector v = new Vector();


Vector is rarely used now-a-days. Usually you use ArrayList.

The complaint is you have not generified your collection.

e.g. Vector<String> v = new Vector<String>(11);

See http://mindprod.com/jgloss/generics.html
http://mindprod.com/jgloss/arraylist.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
GØD is REAL untess declared INTEGER.
~ Anonymous

In FØRTRAN, variables beginning I..N are implicitly INTEGER, the rest REAL.
 
Reply With Quote
 
busyguy busyguy is offline
Junior Member
Join Date: Apr 2011
Posts: 1
 
      04-17-2011
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to eliminate warning: unchecked call to additem(E) java as amember of the raw type javax.swing.JComboBox clusardi2k@aol.com Java 11 09-06-2012 09:21 PM
Xlint " warning: [unchecked] unchecked conversion" RVic Java 19 07-25-2009 08:40 PM
Unchecked cast warning? Knute Johnson Java 4 11-01-2006 10:59 PM
Avoid unchecked cast warning Ian Pilcher Java 0 08-20-2005 05:23 PM
Deserializing in JDK1.5 without unchecked cast warning Andrew Lee Java 2 11-18-2004 03:06 PM



Advertisments