Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
import java.lang.Comparable; import java.lang.ClassCastException; public class SortTest implements Comparable<SortTest>{ private String name; private int rebound; private int block; private int steal; private int points; private int assist; private int turnover; private double average; //constructor public SortTest(){ name = ""; rebound = 0; block = 0; steal = 0; points = 0; assist = 0; turnover = 0; average = 0; } public void setName(String newName) { name = newName; } public String getName() { return name; } public void setRebound(int newRebound) { rebound = newRebound; } public int getRebound() { return rebound; } public void setBlock(int newBlock) { block = newBlock; } public int getBlock() { return block; } public void setSteal(int newSteal) { steal = newSteal; } public int getSteal() { return steal; } public void setPoints(int newPoints) { points = newPoints; } public int getPoints() { return points; } public void setAssist(int newAssist) { assist = newAssist; } public int getAssist() { return assist; } public void setTurnover(int newTurnover) { turnover = newTurnover; } public int getTurnover() { return turnover; } public void setAverage(double rebound, double block, double steal, double points, double assist, double turnover){ average = points*3/10 + assist*2/10 + rebound*2/10 + steal*1/10 + block*1/10 + (turnover+1)*1/10; } public double getAverage(){ return average; } public String toString(){ String str; str = getName() + ": " + getAverage(); return str; } /*public int compareTo(SortTest comparePlayer) { double compareScore = ((SortTest) comparePlayer).getAverage(); return (int) (average - compareScore); }*/ public int compareTo(SortTest otherPlayer) { double compareScore = ((SortTest) otherPlayer).getAverage(); if(this.getAverage() > compareScore){ return 1; } else if (this.getAverage() < compareScore){ return -1; } else return 0; } }
import java.util.Scanner; import java.util.Arrays; public class BasketballPlayer { public static void main(String[] args){ Scanner reader = new Scanner(System.in); SortTest[] player = new SortTest[6]; System.out.println("********BASKETBALL SCORE SHEET********"); for(int playerIndex=0, i=1; playerIndex < 5; playerIndex++){ int[] attribute = new int[6]; String tempName; System.out.println("Enter Player-" + i++ + " information:"); player[playerIndex] = new SortTest(); System.out.print("NAME: "); tempName = reader.next(); player[playerIndex].setName(tempName); System.out.print("POINT/S: "); attribute[0] = reader.nextInt(); player[playerIndex].setPoints(attribute[0]); System.out.print("ASSIST/S: "); attribute[1] = reader.nextInt(); player[playerIndex].setAssist(attribute[1]); System.out.print("REBOUND/S: "); attribute[2] = reader.nextInt(); player[playerIndex].setRebound(attribute[2]); System.out.print("STEAL/S: "); attribute[3] = reader.nextInt(); player[playerIndex].setSteal(attribute[3]); System.out.print("BLOCK/S: "); attribute[4] = reader.nextInt(); player[playerIndex].setBlock(attribute[4]); System.out.print("TURNOVER/S: "); attribute[5] = reader.nextInt(); player[playerIndex].setTurnover(attribute[5]); player[playerIndex].setAverage(attribute[0], attribute[1], attribute[2], attribute[3], attribute[4], attribute[5]); } Arrays.sort(player); for(int playerIndex=0; playerIndex < 5; playerIndex++){ SortTest playerPrint = player[playerIndex]; System.out.println(playerPrint.toString()); } } }

If I'm correct you may be gettin Null pointer exception.
In that case u the prob is that you are creating array SortTest[] player = new SortTest[6]; and not object instance.
Try by creating instance of that object
SortTest st = new SortTest();
if u want to create objects on run time use factory pattern method.
Hope this works .