Warning unchecked call

Discussion in 'Java' started by jc_usernet, Jan 22, 2010.

  1. jc_usernet

    jc_usernet Guest

    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.....
     
    jc_usernet, Jan 22, 2010
    #1
    1. Advertising

  2. jc_usernet

    Nigel Wade Guest

    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
     
    Nigel Wade, Jan 22, 2010
    #2
    1. Advertising

  3. jc_usernet

    Lew Guest

    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
     
    Lew, Jan 22, 2010
    #3
  4. jc_usernet

    markspace Guest

    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>();
     
    markspace, Jan 22, 2010
    #4
  5. jc_usernet

    Lew Guest

    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
     
    Lew, Jan 22, 2010
    #5
  6. jc_usernet

    markspace Guest

    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.
     
    markspace, Jan 22, 2010
    #6
  7. jc_usernet

    Lew Guest

    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
     
    Lew, Jan 22, 2010
    #7
  8. jc_usernet

    Roedy Green Guest

    On Fri, 22 Jan 2010 02:31:43 -0800 (PST), jc_usernet
    <> 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.
     
    Roedy Green, Jan 23, 2010
    #8
  9. jc_usernet

    busyguy

    Joined:
    Apr 17, 2011
    Messages:
    1
    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.
     
    busyguy, Apr 17, 2011
    #9
    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,142
    Andrew Lee
    Nov 18, 2004
  2. Ian Pilcher

    Avoid unchecked cast warning

    Ian Pilcher, Aug 20, 2005, in forum: Java
    Replies:
    0
    Views:
    3,565
    Ian Pilcher
    Aug 20, 2005
  3. Knute Johnson

    Unchecked cast warning?

    Knute Johnson, Nov 1, 2006, in forum: Java
    Replies:
    4
    Views:
    1,036
    Knute Johnson
    Nov 1, 2006
  4. RVic
    Replies:
    19
    Views:
    1,434
  5. Replies:
    11
    Views:
    4,627
    sameer k
    Sep 6, 2012
Loading...

Share This Page