2
I’m having difficulties in using the similar interface, I must perform the increasing comparison of drivers according to their score (class PilotoEquipe
), and then in class CorridaX1
, need to use the Arrays.sort
to organize the pilot array. When running the Arrays.sort
I get the following error:
Exception in thread "main" java.lang.Nullpointerexception at java.util.Comparabletimsort.countRunAndMakeAscending(Comparabletimsort.java:316) at java.util.Comparabletimsort.Sort(Comparabletimsort.java:184) at java.util.Arrays.Sort(Arrays.java:1246) at Leonardobruksch.Corridax1.orderna(Corridax1.java:41) at Leonardobruksch.ControlionFederacao.main(Controlpower.java:35) Java Result: 1
Class Pilotoequipe:
public class PilotoEquipe implements PilotoEquipeInterface, Comparable<PilotoEquipe>{
protected String nome;
protected String equipe;
protected double pontos;
public PilotoEquipe(String nome, String equipe, double pontos){
this.nome = nome;
this.equipe = equipe;
this.pontos = pontos;
}
@Override
public String getNome() {
return nome;
}
@Override
public void setNome(String nome) {
this.nome = nome;
}
@Override
public String getEquipe() {
return equipe;
}
@Override
public void setEquipe(String equipe) {
this.equipe = equipe;
}
@Override
public double getPontos() {
return pontos;
}
@Override
public void setPontos(double pontos) {
this.pontos = pontos;
}
public int compareTo(PilotoEquipe piloto) {
if(this.getPontos() > piloto.getPontos())
return -1;
else if(this.getPontos() == piloto.getPontos())
return 0;
else
return 1;
}
}
Corridax1:
public class CorridaX1 {
PilotoEquipeInterface[] p = new PilotoEquipeInterface[20];
public void exibePilotos(){
int i = 0;
while(p[i] != null && i<p.length){
int numero = i+1;
System.out.println("Piloto "+numero+":");
System.out.println("Nome: "+p[i].getNome());
System.out.println("Equipe: "+p[i].getEquipe());
System.out.println("----------------------------");
i++;
}
}
public void orderna(){
Arrays.sort(p);
}
}
Class Control Power(main)
public class ControleFederacao {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
CorridaX1 c = new CorridaX1();
System.out.println("Digite o nome: ");
String nome = scanner.nextLine();
System.out.println("Digite a equipe: ");
String equipe = scanner.nextLine();
System.out.println("Digite os pontos do piloto: ");
double pontos = scanner.nextDouble();
PilotoEquipe piloto = new PilotoEquipe(nome, equipe, pontos);
c.p[0] = piloto;
System.out.println("Antes da Ordenacao: ");
c.exibePilotos();
System.out.println("Apos a Ordenacao: ");
c.orderna();
c.exibePilotos();
}
}
Place the contents of the Control.java class and where is line 35 so we can help you. Being Nullpointer, I believe error is simple.
– Lucas Polo
The error is in logic within its main method, in the Control-Factor class. Please click [Edit] and add the code of this class so that we can help.
– Math
Be careful when instantiating this 20 position array, if you leave any unfilled positions will cause this NPE. If the number of objects in the class
PilotoEquipeInterface
is dynamic use aArrayList
for example.– Wakim
As already said, most likely your problem is null positions in the array (since the error occurs in itself
sort
, and not in your code). And I would like to complement with a detail: avoid returning0
in thecompareTo
unless the objects are in fact equal (i.e.a.equals(b)
returntrue
). In your case this is not so important, because you are only using the methodsort
, but if you used these objects as the key to aTreeSet
,TreeMap
or similar behaviour (inconsistency between thecompareTo
and theequals
) would cause problems. Choose a "tiebreaker criterion"...– mgibsonbr
I put my main class (Control Power) up there too! Thanks a lot!
– leonardokbruksch