Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

wanted: JAVA Gurus

Status
Not open for further replies.

compe718

Technical User
Mar 20, 2002
2
US
I've spent hours look everywhere on the internet, books, java api docs for help, but can't seem to get good explaination. i really need someone to explain it to me. i need to sort an array of objects. I know i have to implement the comparable interface, but can't understand how it works. for anyone who can give me some help, i will give you my first born.
this is as far as i got... don't know where to go from here.
Thanks in advance

My program consists of two classes. I have an array of objects i want to sort. At runtime it gave me a NullExpection... something.
//first class
.
.
.
Arrays.sort(myArray); //in main

------------------------------------------------------------
// my second class
class myClass implements Comparable
.
.
.
public int compareTo(Object obj)
{
Customer c=(Customer) obj;
if(this.c>c.amount)
return 1;
else if(this.amount<c.amount)
return -1;
else return 0;
}
}
 
The problem may be with the line

if(this.c>c.amount)

I think what you intended to code is

if(this.c.amount>c.amount)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top