Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unchecked or Unsafe Operations

    Hello, I am relatively new to Java, but I have a friend whose helping me however, I have run into a problem that he doesn't know how to fix. I get an warning when I compile, but it seems like everything is working still. The Warning says:

    Note: [classpath...]/EditorGUI.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.

    As I said, it seems like my program still works, but is there some way to get rid of this warning? Am I doing something wrong? I found out which line causes the warning, and I googled the error, but as I'm somewhat new to Java, I was unable to take what I found and apply it to my program. Here is the different parts of code associated with the warning.

    In the top part of my class where I declare all my variables, I declare this:
    Code:
    public ArrayList<TagInstance> bitmTags;
    //TagInstance is another one of my classes, which I wish to create an array of
    Then I instantiate the ArrayList later on.
    Code:
    bitmTags = new ArrayList<TagInstance>();
    And finally, here is the line that causes the warning, because when I comment it out, I don't get the warning, but I need to use this function to make my program work like I want it to.
    Code:
    Collections.sort(bitmTags);
    Is there something I am doing wrong, and is there some way to get rid of this warning, or should I just live with it, seeing as my program still works even when I get the warning?

  • #2
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    I believe I have a solution for you. For the sort method to be considered "safe" all of the items in the List(ArrayList in this case) must implement comparable. Because you wrote the TagInstance class, it may not implement the comparable interface. Look at the sort method at this link:
    http://java.sun.com/j2se/1.5.0/docs/...esindex-1.html

  • #3
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Actually, TagInstance already does implement comparable. I forgot to point that out in the first post. Here is the compareTo method in TagInstance:
    Code:
    public int compareTo(Object o) {
            TagInstance other = (TagInstance)o;
            return this.tagName.compareToIgnoreCase(other.tagName);
            //tagName is a String variable in the TagInstance class.
        }
    Thanks for trying to help though, sorry I forgot to point that out earlier. Does anyone have any other suggestions?

  • #4
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    You're right. You're implementation does indeed implement comparable.. BUT it does not implement a safe comparable as far as the compiler is concerned.

    Since you are passing in an Object of type o and forcing a cast, the compiler see's this as unsafe since in theory you can pass anything through it. The trick is implement comparable with a generic type.

    So you can do this one of two ways depending on how you're using comparable. If you're implementing it, it's just a matter of specifying the type (in this case a TagInstance

    To give you a quick example using Strings:

    Code:
    import java.util.*;
    
    public class testCompare implements Comparable<String>{	
    	public int compareTo(String s)
    	{
    		return 0;
    	}
    }
    That should compile it with no errors regarding compareTo. Unless of course you are doing some more unchecked conversions
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #5
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks Aradon! That took care of the warning.

  • #6
    New to the CF scene
    Join Date
    Apr 2007
    Location
    Canada
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Same problem

    Hate to revive an old thread, but I needed a little advice on the same problem.

    I get the same warning messages:
    Note: [classpath...]/EditorGUI.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.


    But my warning message occurs when I use the "set" method when using an ArrayList.

    In particular:

    Code:
    	public void setCoefficient(int index, BigInteger number) {
    		coefficient.set(index, number);
    	}
    Where coefficient is just an ArrayList.
    I tried to place "<BigInteger>" in different places, but still no luck.

    I tried googling the warning when associated with the "set" method but no luck.

    Any info would be greatly appreciated!

  • #7
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    As the post above suggests, this warning does not seem to just plague classes that don't implement Comparable. I've seen this warning plenty of times using things like Vector<Double> etc.

    Both Double and BigInteger implement Comparable and provide CompareTo methods (which I assume would be "safe")

    Am I missing understanding the issue here?

  • #8
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    When you recompile with -Xlnit what are the error messages that come up? Also can you post us the whole class, or at the least where you create and instantiate the ArrayList?

    As the post above suggests, this warning does not seem to just plague classes that don't implement Comparable. I've seen this warning plenty of times using things like Vector<Double> etc.
    There are all sorts of reasons to get this warning. And you are right,it doesn't just plague classes that don't implement Comparable. In theory it can plague all classes. It's just a matter of figuring out what it's talking about.
    Last edited by Aradon; 04-11-2007 at 07:18 AM.
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #9
    New to the CF scene
    Join Date
    Apr 2007
    Location
    Canada
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the input people!

    Well just like Gox mentioned, I'm also confused as to why the warning appears, as Double and BigInteger implement the Comparable class.

    Aradon, this is where I instantiate the ArrayList:

    Code:
    	public Polynomial(int theDegree, ArrayList theCoefs, BigInteger modClass) {
    		degree = theDegree;
    		modulo = mocClass;
    		
    		ArrayList<BigInteger> coefficients = new ArrayList<BigInteger>(theCoefs.size());
    		for(int i=0; i<theCoefs.size(); i++) {
    			coefficients.add(((BigInteger)theCoefs.get(i)).mod(modulo));
    		}
    	}
    I guess I should say that I'm creating a Polynomial class, and coefficients is the ArrayList that carries the coefficients of the polynomial.

    And where I get the error is this line:

    Code:
    	public void setCoefficient(int index, BigInteger number) {
    		coefficients.set(index, number);
    	}
    I know this warning probably won't make a difference when I run it, but I think as programmers, we'd all like our codes warning-free!

  • #10
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    I'm sorry, you must have misunderstood what I was saying. When I said recompile what I was saying was recompile it with the flag. That is

    Code:
    javac -Xlint:unchecked something.java
    Where something.java is your code, etc.

    I am also confused on one point in your code. You create a new ArrayList called coefficients in your constructor, but I don't see it as global, public, private, static, anything. So my question is how are you doing a set on it at all?
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #11
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Avoiding the warning

    if you are sure you're doing everything right and you don't want to compiler to shout about Generic related matters you should write this before the method :
    @SuppressWarnings("unchecked")

    and you will not get the warnings.

    for further info :
    http://java.sun.com/j2se/1.5.0/docs/.../features.html

    (check Generics and Metadata )

    Good luck,Tal

  • #12
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    Quote Originally Posted by taluk View Post
    if you are sure you're doing everything right and you don't want to compiler to shout about Generic related matters you should write this before the method :
    @SuppressWarnings("unchecked")

    and you will not get the warnings.

    for further info :
    http://java.sun.com/j2se/1.5.0/docs/.../features.html

    (check Generics and Metadata )

    Good luck,Tal
    Well yes, but that doesn't get rid of the warning which could be a legitimate warning. That's why I was asking if he would recompile it.
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #13
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am also having a problem with my class, i dont understand what is going on as it all looks fine to me.

    Code:
    package javaapplication2;
    
    import java.util.*;
    import java.io.*;
    
    public class HighscoreManager {
        // An arraylist of the type "score" we will use to work with the scores inside the class
        private ArrayList<Score> scores;
    
        // The name of the file where the highscores will be saved
        private final String HIGHSCORE_FILE = "scores.dat";
    
        //Initialising an in and outputStream for working with the file
        ObjectOutputStream outputStream = null;
        ObjectInputStream inputStream = null;
    
        public HighscoreManager() {
            //initialising the scores-arraylist
            scores = new ArrayList<Score>();
        }
    
        public ArrayList<Score> getScores() {
            loadScoreFile();
            sort();
            return scores;
        }
    
    private void sort() {
            ScoreComparator comparator = new ScoreComparator();
            Collections.sort(scores, comparator);
    }
    
    
    public void addScore(int score) {
            loadScoreFile();
            scores.add(new Score(score));
            updateScoreFile();
    }
    
    public void loadScoreFile() {
            try {
                inputStream = new ObjectInputStream(new FileInputStream(HIGHSCORE_FILE));
                scores = (ArrayList<Score>) inputStream.readObject();
            } catch (FileNotFoundException e) {
                System.out.println("[Laad] FNF Error: " + e.getMessage());
            } catch (IOException e) {
                System.out.println("[Laad] IO Error: " + e.getMessage());
            } catch (ClassNotFoundException e) {
                System.out.println("[Laad] CNF Error: " + e.getMessage());
            } finally {
                try {
                    if (outputStream != null) {
                        outputStream.flush();
                        outputStream.close();
                    }
                } catch (IOException e) {
                    System.out.println("[Laad] IO Error: " + e.getMessage());
                }
            }
    }
    
    public void updateScoreFile() {
            try {
                outputStream = new ObjectOutputStream(new FileOutputStream(HIGHSCORE_FILE));
                outputStream.writeObject(scores);
            } catch (FileNotFoundException e) {
                System.out.println("[Update] FNF Error: " + e.getMessage() + ",the program will try and make a new file");
            } catch (IOException e) {
                System.out.println("[Update] IO Error: " + e.getMessage());
            } finally {
                try {
                    if (outputStream != null) {
                        outputStream.flush();
                        outputStream.close();
                    }
                } catch (IOException e) {
                    System.out.println("[Update] Error: " + e.getMessage());
                }
            }
    }
    
    public String getHighscoreString() {
            String highscoreString = "";
    	int max = 10;
    
            ArrayList<Score> scores;
            scores = getScores();
    
            int i = 0;
            int x = scores.size();
            if (x > max) {
                x = max;
            }
            while (i < x) {
                highscoreString += (i + 1) + "\t\t" + scores.get(i).getScore() + "\n";
                i++;
            }
            return highscoreString;
    }
    }

  • #14
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I apologize for the bump, but I felt it appropriate to add my solution to this problem. This thread is very high on Google when searching for the aforementioned error message.

    I recompiled with the flag it suggests and my error came from this "bad" line of code:

    Code:
    ArrayList<JButton> sodaChoices = new ArrayList();
    The error disappeared when I wrote this:

    Code:
    ArrayList<JButton> sodaChoices = new ArrayList<JButton>();
    Sorry if "bumping" peeved anyone, but what would the internet be if people didn't share knowledge? Hopefully this helps some other poor intro Java student out there like myself


  •  

    Posting Permissions

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