aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Comparator " Watch "Comparator " New topic
Author

Comparator

Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

Hi...

I'm getting the error :"int cannot be dereferenced" for the following code:



What is the problem??

===>SCJP 1.5(72%)<===
==>SCWCD1.5(76%)<===
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

i think a.getFirst() method is returning int.
using int you can't call a method ...
you are doing something like 5.compareTo()


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

Nope...

getFirst() returns a String..first name
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Just use a cast if a.getFirst actually returns an int

((Integer)a.getFirst()).compareTo(b.getFirst())


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

Look:

James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

we have given the solution , now go through the code again
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

I think this code will give the error

46. return (a.getAge()).compareTo(b.getAge());

and not this one

56. return (a.getFirst()).compareTo(b.getFirst());
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

Hmm doesn't int implement Comparable?
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

Maybe I should change to Integer
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Duran Harris wrote:Hmm doesn't int implement Comparable?


int is a primitive datatype. It cannot implement anything
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Ankit Garg wrote:Just use a cast if a.getFirst actually returns an int

((Integer)a.getFirst()).compareTo(b.getFirst())


this is better option
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

Ok that's fixed...

but now I have a null pointer exception in populateArray() ops:
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Did you call createCustomer before calling populateArray??
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

yes,and I can see the customers.txt file that has been created...
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Nothing
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

well I changed the customer[] to an arraylist and it works..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparator