public class CollectionsDemo4 {
public static void main(String a[]){
List<Empy> emps = new ArrayList<Empy>();
Empy empobj1=new Empy(10, "Raghu", 25000);
Empy empobj2=new Empy(120, "Krish", 45000);
Empy empobj3=new Empy(140, "John", 14000);
Empy empobj4=new Empy(14000, "Kishore", 24000);
emps.add(empobj1);
emps.add(empobj2 );
emps.add(empobj3 );
emps.add(empobj4 );
Empy minSal = Collections.min(emps, new EmpyComp());
System.out.println("Employee with min salary: "+minSal);
for (Object value : emps) {
System.out.println("Value = " + value);
}
System.out.println("The initial list is :"+emps.toString());
Comparator<Empy> cmp = Collections.reverseOrder(new EmpyComp() );
System.out.println("");
Collections.sort(emps, cmp);
System.out.println("The sorted reverse list is :"+emps.toString());
Integer number = null;
int b = number.intValue();
int index = Collections.binarySearch(emps, new Empy(b, null,14000), cmp);
System.out.println("Found at index " + index);
}
}
class EmpyComp implements Comparator<Empy>{
@Override
public int compare(Empy e1, Empy e2) {
return e1.getSalary().compareTo(e2.getSalary());
}
}
class Empy{
......
public String toString(){//
return id+" "+name+" "+salary;
}
}
int index = Collections.binarySearch(emps, new Empy(b, null,14000), cmp);
Throw the Exception in thread "main" java.lang.NullPointerException. This is the full trace.
How do we let the code just search 3rd column which is salary?