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?
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
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?
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
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.
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")
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?
Quote:
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.
__________________
"To iterate is human, to recurse divine." -L. Peter Deutsch
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
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")
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")
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