Results 1 to 4 of 4
  1. #1
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default Unchecked or unsafe operations warning

    Hi everyone,

    When I compile my code, the compiler returns that it has unsafe or uncheck operations. Then, I re-compile it with the option -Xlint:unchecked, and I get all this warnings:

    Java Code:
    javac -Xlint:unchecked ExecuteCommand.java
    ExecuteCommand.java:16: warning: [unchecked] unchecked conversion            
    found   : java.util.ArrayList                                                
    required: java.util.ArrayList<Cinema>                                        
        private ArrayList<Cinema> cinemas = new ArrayList();                     
                                            ^                                    
    ExecuteCommand.java:17: warning: [unchecked] unchecked conversion            
    found   : java.util.ArrayList                                                
    required: java.util.ArrayList<Film>                                          
        private ArrayList<Film> films = new ArrayList();                         
                                        ^                                        
    ExecuteCommand.java:18: warning: [unchecked] unchecked conversion            
    found   : java.util.ArrayList                                                
    required: java.util.ArrayList<Booking>                                       
        private ArrayList<Booking> bookings = new ArrayList();                   
                                              ^                                  
    ExecuteCommand.java:270: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
                                outputArray.add(getNameOfDay(j));                                                         
                                               ^                                                                          
    ExecuteCommand.java:309: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
                        outputArray.add(cinema.getName());                                                                
                                       ^                                                                                  
    ExecuteCommand.java:348: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
                output.add(bookingID);                                                                                    
                          ^                                                                                               
    ExecuteCommand.java:367: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
                output.add(booking.getFilmName());                                                                        
                          ^                                                                                               
    ExecuteCommand.java:368: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
                output.add(booking.getCinemaName());                                                                      
                          ^
    ExecuteCommand.java:369: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
                output.add(getNameOfDay(booking.getDay()));
                          ^
    ExecuteCommand.java:370: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.ArrayList
                output.add(booking.getSeats());
                          ^
    10 warnings
    Any idea about what is happening with my code? Thanks in advance.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    found   : java.util.ArrayList                                                
    required: java.util.ArrayList<Cinema>                                        
        private ArrayList<Cinema> cinemas = new ArrayList();

    The error message means what it says. You had written

    Java Code:
    private ArrayList<Cinema> cinemas = new ArrayList();

    but the compiler saw that you had declared cinemas as ArrayList<Cinema> and, so, expected you to call the appropriate constructor:

    Java Code:
    private ArrayList<Cinema> cinemas = new ArrayList<Cinema>();

    The original warning was just that: a warning. You can ignore it if you want but it is considered a bad programming practice to use the raw type (ArrayList without the <Cinema>) unless you need to because you are using methods from older (nongeneric) libraries.

    See Type Erasure (The Java™ Tutorials > Learning the Java Language > Generics) (and possibly more of the surrounding chapter on generics.)

  3. #3
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    Ok, I've already fixed the three first warnings (indeed I should read more carefully the warnings, but I didn't know that kind of constructor exists). Now I'm going to read the link to see if I can fix the other ones. I appreciate the help and thanks for the link.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Again I think it's that outputArray and output have been (declared and) initialised as ArrayList rather than ArrayList<String> or whatever they are supposed to be array lists of.

Similar Threads

  1. Conversion Warning
    By kishan in forum Advanced Java
    Replies: 6
    Last Post: 10-10-2009, 01:52 PM
  2. Replies: 22
    Last Post: 05-08-2008, 12:00 PM
  3. String operations..
    By sireesha in forum New To Java
    Replies: 4
    Last Post: 12-14-2007, 02:04 AM
  4. Uses unchecked or unsafe operations message
    By Robbinz in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 10:56 PM
  5. UnChecked Exception
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-18-2007, 07:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •