Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-23-2007, 10:28 PM   PM User | #1
TunesRus90
New to the CF scene

 
Join Date: Jan 2007
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
TunesRus90 is an unknown quantity at this point
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?
TunesRus90 is offline   Reply With Quote
Old 01-24-2007, 09:08 PM   PM User | #2
brad211987
Regular Coder

 
brad211987's Avatar
 
Join Date: Sep 2005
Location: Ohio
Posts: 631
Thanks: 10
Thanked 50 Times in 50 Posts
brad211987 is an unknown quantity at this point
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
brad211987 is offline   Reply With Quote
Old 01-24-2007, 09:51 PM   PM User | #3
TunesRus90
New to the CF scene

 
Join Date: Jan 2007
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
TunesRus90 is an unknown quantity at this point
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?
TunesRus90 is offline   Reply With Quote
Old 01-24-2007, 10:16 PM   PM User | #4
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
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
Aradon is offline   Reply With Quote
Old 01-24-2007, 11:22 PM   PM User | #5
TunesRus90
New to the CF scene

 
Join Date: Jan 2007
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
TunesRus90 is an unknown quantity at this point
Thanks Aradon! That took care of the warning.
TunesRus90 is offline   Reply With Quote
Old 04-10-2007, 10:51 PM   PM User | #6
swong7
New to the CF scene

 
Join Date: Apr 2007
Location: Canada
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
swong7 is an unknown quantity at this point
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!
swong7 is offline   Reply With Quote
Old 04-11-2007, 03:43 AM   PM User | #7
Gox
Regular Coder

 
Gox's Avatar
 
Join Date: May 2006
Location: Ontario, Canada
Posts: 392
Thanks: 2
Thanked 20 Times in 20 Posts
Gox will become famous soon enough
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?
Gox is offline   Reply With Quote
Old 04-11-2007, 06:11 AM   PM User | #8
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
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

Last edited by Aradon; 04-11-2007 at 06:18 AM..
Aradon is offline   Reply With Quote
Old 04-11-2007, 05:39 PM   PM User | #9
swong7
New to the CF scene

 
Join Date: Apr 2007
Location: Canada
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
swong7 is an unknown quantity at this point
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!
swong7 is offline   Reply With Quote
Old 04-11-2007, 11:23 PM   PM User | #10
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
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
Aradon is offline   Reply With Quote
Old 04-15-2007, 07:03 AM   PM User | #11
taluk
New to the CF scene

 
Join Date: Mar 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
taluk is an unknown quantity at this point
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
taluk is offline   Reply With Quote
Old 04-15-2007, 04:16 PM   PM User | #12
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
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
Aradon is offline   Reply With Quote
Old 09-23-2009, 06:17 PM   PM User | #13
Cloakd
New to the CF scene

 
Join Date: Sep 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Cloakd is an unknown quantity at this point
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;
}
}
Cloakd is offline   Reply With Quote
Old 12-02-2011, 02:48 AM   PM User | #14
mike1305
New to the CF scene

 
Join Date: Dec 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
mike1305 is an unknown quantity at this point
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
mike1305 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 02:57 PM.


Advertisement
Log in to turn off these ads.