This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I'm working on this project that I need to override the compareTo() method in the Comparable interface in one class and create a new TreeSet in another using the first class. What i've written eclipse doesn't have a problem with, but when I try to create a TreeSet in the second class it I get this error:
Exception in thread "main" java.lang.ClassCastException: NameRank cannot be cast to java.lang.String at NameRank.compareTo(NameRank.java:1)
my compareTo() class is supposed to set the “Natural Ordering” of the class to alphabetical by the Name field. mine looks like this:
public int compareTo(String o)
int res = 0;
if (this.name.compareTo(o) < 0)
res = -1;
if (this.name == o)
res = 0;
if (this.name.compareTo(o) > 0)
res = 1;
Your NameRank class seems to implements Comparator<String> ? That's kinda wrong -- it should implement Comparator<NameRank>. After all, your TreeSet contains NameRank objects, so a NameRank object needs to compare to another NameRank object.
"Research" or no, the stack trace doesn't lie. It shows exactly what code is excuting when the error occurs. It shows you calling add() on a TreeSet at line 200 of your program (it's numbered 201 here but that's probablyjust a cut-and-paste thing.) TreeSet.add() calls TreeMap.put() (since a TreeSet is just a TreeMap internally) and then the NullPointerException occurs. What this means is that you're calling add() with "null" as an argument. That's pretty obvious -- the array "last" is allocated at a fixed size, but the number of elements added to it is determined by an external file. The loop that reads from the file is pretty dodgy, and all those empty catch blocks mean that if something goes wrong and not a single line is properly read, we won't actually know about it; the errors will just be ignored. "compareTo" is absolutely not involved.
Note that you can add "null" to any List implementation legally, and HashSet (HashMap) has a special provision for adding a null key. Of the collections you use, then, TreeSet is the first one sensitive to "null" being added, so that's where the problem appears. But frankly, who knows where the nulls are coming from. Is the array supposed to be exactly the right size to hold the records from the file? Why not size the array based on the number of records you find, instead (collect the lines in a List, and only when you know the count, then allocate an array.)
So to reiterate my main points:
1) Stack traces don't lie. Don't imagine the problem is somewhere else when the stack trace tells you precisely where the error occurs.
2) Read the Javadocs for the classes you use so you know (for example) how they handle nulls!
3) Computers are NOT mysterious. They are machines with absolutely deterministic behavior. If you don't know why something happens, find out.
4) NEVER HAVE EMPTY CATCH BLOCKS!
And one more point: never, ever, write a couple hundred lines of code and then push them off a cliff, as you've done here. There's too much code here to now go back and fix. Write a few, compile, test. Then write a few more, compile, and test. Don't move forward with step 6 until step 5 is perfect. Add print statements temporarily to help you understand exactly what your code is doing, or step through it with a debugger. Never be mystified -- always make sure you understand!
Seriously, use JUnit. Both Eclipse and Netbeans will generate the tests for you with a click or two.
Do it early, test often.
It really will make your life easier.
Joined: Oct 17, 2009
The arrays are sized to two exact files my professor gave us and the program is built to only read those two files. What I don't get is how can there be a Null Pointer Exception if every one of the previous data structures (Array List, Hash Set, Linked List) can read the data fine before getting to the TreeSet? When I comment out the whole TreeSet benchmarking section the program runs fine, just when it is uncommented do I get a null pointer exception. Is there some kind of special rule for TreeSets I don't know about and need to implement in my code?
Nathan Doe wrote:The arrays are sized to two exact files my professor gave us and the program is built to only read those two files. What I don't get is how can there be a Null Pointer Exception if every one of the previous data structures (Array List, Hash Set, Linked List) can read the data fine before getting to the TreeSet? When I comment out the whole TreeSet benchmarking section the program runs fine, just when it is uncommented do I get a null pointer exception. Is there some kind of special rule for TreeSets I don't know about and need to implement in my code?
Step 1. Stop speculating. Use the NPE to locate the exact line of the exception. Then add logging, or printouts to confirm that the reference is actually null. Confirm the null, then figure out how it happened.
Joined: Oct 17, 2009
Ok, so it looks like I made the Array "last" one spot too big which the TreeSet didn't like. Im guessing that TreeSets don't accept null values for positions in them where as the other data structures just store the null value.
Nathan Doe wrote:Ok, so it looks like I made the Array "last" one spot too big which the TreeSet didn't like. Im guessing that TreeSets don't accept null values for positions in them where as the other data structures just store the null value.
I believe, and I may be wrong, the TreeSet class can take a null, if you are using a Comparator. If you are using Comparable elements, then you'll get a NullPointerException because the TreeSet can't compare the element.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: Implementing Comparable Interface with TreeSets