Comparable with Arrays.Sort

Asked

Viewed 92 times

1

I’m having difficulties using the interface Comparable. In class TrabalhoGA I must call the method ordenaPorPontos class temporada2013 comparing the points of Pilotos of this class, I 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 work.porPontos.compare(porPontos.java:21) at java.util.Timsort.binarySort(Timsort.java:265) at java.util.Timsort.Sort(Timsort.java:190) at java.util.Timsort.Sort(Timsort.java:173) at java.util.Arrays.Sort(Arrays.java:659) at work.temporada2013.ordenaPorPontos(temporada2013.java:49) Main(Work.java:56) Java Result: 1

Classe Trabalho:

    Piloto[] pontuaram = new Piloto[30];

public void inserePilotosPontuados(String driver, String team, int points) {
    for(int i=0;i<getLast();i++){
        if(pontuaram[i].getDriver().equalsIgnoreCase(driver)){
            pontuaram[i].setPts(pontuaram[i].getPts()+points);
            return;
        }

    }
    pontuaram[getLast()] = new Piloto(driver, team, points);
}

private int getLast() {
int last = 0;

for (int i = 0; i < pontuaram.length; i++) {
    if (pontuaram[i] == null) {
        last = i;
        return last;
    }
}

return last;
}

public void ordenaPorNome(){  
    Arrays.sort(pontuaram,new porNome());  
}  

public void ordenaPorPontos(){  
    Arrays.sort(pontuaram,new porPontos());  
}  

public void gravar(File arquivo)throws IOException{  
    PrintWriter gravar = new PrintWriter(new FileWriter(arquivo));  

    for (int i=0;i<pontuaram.length;i++){ 
        gravar.print(pontuaram[i]);  
    }
    gravar.close();  
}

public void mostraNaTela(){  
    int i = 0;
    while(pontuaram[i] != null && i<pontuaram.length){
        int numero = i+1;
        System.out.println("Classificado em "+numero+"° lugar:");
        System.out.println("Nome: "+pontuaram[i].getDriver());
        System.out.println("Equipe: "+pontuaram[i].getTeam());
        System.out.println("Pontos: "+pontuaram[i].getPts());
        System.out.println("----------------------------");
        i++;
    }
}

Sort Class:

    public int compare(Object obj1, Object obj2){  
    Piloto p1 = (Piloto) obj1;  
    Piloto p2 = (Piloto) obj2;  

    if (p1.getPts() == p2.getPts())  
        return 0;  
    else if (p1.getPts() > p2.getPts())  
        return -1;  
    else  
        return 1;  

P1 = work. Pilot@6f54c08a, P2 = work.Pilot@252cdd20 P1 = work. Pilot@246972f1, P2 = work.Pilot@6f54c08a P1 = work. Pilot@6f93ee4, P2 = work.Pilot@246972f1 P1 = work. Pilot@6f93ee4, P2 = work.Pilot@6f54c08a P1 = work. Pilot@6f93ee4, P2 = work.Pilot@252cdd20 P1 = work. Pilot@558fee4f, P2 = work.Pilot@6f54c08a P1 = work. Pilot@558fee4f, P2 = work.Pilot@252cdd20 P1 = work. Pilot@558fee4f, P2 = work.Pilot@6f93ee4 P1 = work. Pilot@5c66b06b, P2 = work.Pilot@252cdd20 P1 = work. Pilot@5c66b06b, P2 = work.Pilot@558fee4f P1 = work. Pilot@5c66b06b, P2 = work.Pilot@6f93ee4 P1 = work. Pilot@59c87031, P2 = work.Pilot@252cdd20 P1 = work. Pilot@59c87031, P2 = work.Pilot@246972f1 P1 = work. Pilot@59c87031, P2 = work.Pilot@6f54c08a P1 = work. Pilot@763dcf03, P2 = work.Pilot@252cdd20 P1 = work. Pilot@763dcf03, P2 = work.Pilot@5c66b06b P1 = work. Pilot@763dcf03, P2 = work.Pilot@558fee4f P1 = work. Pilot@53e20a9a, P2 = work.Pilot@252cdd20 P1 = work. Pilot@53e20a9a, P2 = work.Pilot@763dcf03 P1 = work. Pilot@53e20a9a, P2 = work.Pilot@5c66b06b P1 = work. Pilot@1d262f7c, P2 = work.Pilot@558fee4f P1 = work. Pilot@1d262f7c, P2 = work.Pilot@53e20a9a P1 = work. Pilot@1d262f7c, P2 = work.Pilot@763dcf03 P1 = work. Pilot@35f784d7, P2 = work.Pilot@558fee4f Exception in thread "main" java.lang.Nullpointerexception P1 = work. Pilot@35f784d7, P2 = work.Pilot@59c87031 P1 = work. Pilot@35f784d7, P2 = work.Pilot@6f54c08a P1 = work. Pilot@35f784d7, P2 = work.Pilot@252cdd20 P1 = work. Piloto@d 325aef, P2 = work.Pilot@558fee4f P1 = work. Piloto@d 325aef, P2 = work.Pilot@6f54c08a P1 = work. Piloto@d 325aef, P2 = work.Pilot@35f784d7 P1 = work. Pilot@64f007ad, P2 = work.Pilot@252cdd20 P1 = work. Pilot@64f007ad, P2 = work.Pilot@763dcf03 P1 = work. Pilot@64f007ad, P2 = work.Pilot@558fee4f P1 = work. Pilot@64f007ad, P2 = work.Pilot@1d262f7c P1 = work. Pilot@4a8822a0, P2 = work.Pilot@558fee4f P1 = work. Pilot@4a8822a0, P2 = work.Pilot@6f54c08a P1 = work. Pilot@4a8822a0, P2 = work.Pilot@246972f1 at work.porPontos.compare(porPontos.java:22) P1 = work. Pilot@2f995c9a, P2 = work.Pilot@252cdd20 P1 = work. Pilot@2f995c9a, P2 = work.Pilot@59c87031 at java.util.Timsort.binarySort(Timsort.java:265) P1 = work. Pilot@2f995c9a, P2 = work.Pilot@4a8822a0 P1 = work. Pilot@7d8e9adf, P2 = work.Pilot@252cdd20 P1 = work. Pilot@7d8e9adf, P2 = work.Pilot@59c87031 P1 = work. Pilot@7d8e9adf, P2 = work.Pilot@4a8822a0 P1 = work. Pilot@7d8e9adf, P2 = work.Pilot@2f995c9a P1 = work. Pilot@1d59e6df, P2 = work.Pilot@35f784d7 P1 = work. Pilot@1d59e6df, P2 = work.Pilot@246972f1 at java.util.Timsort.Sort(Timsort.java:190) P1 = work. Pilot@1d59e6df, P2 = work.Pilot@2f995c9a P1 = work. Pilot@1d59e6df, P2 = work.Pilot@7d8e9adf P1 = work. Pilot@79444986, P2 = work.Pilot@35f784d7 P1 = work. Pilot@79444986, P2 = work.Pilot@4a8822a0 P1 = work. Pilot@79444986, P2 = work.Pilot@7d8e9adf at java.util.Timsort.Sort(Timsort.java:173) P1 = work. Pilot@79444986, P2 = work.Pilot@2f995c9a P1 = null, P2 = work. Piloto@d 325aef at java.util.Arrays.Sort(Arrays.java:659) at work.temporada2013.ordenaPorPontos(temporada2013.java:49) Main(Work.java:56) Java Result: 1

  • The error is clear: java.lang.Nullpointerexception. Open your porPontos.java class (which you should probably implement java.lang.Comparable) and take it easy to see that probably one of the two parameters passed in the compare method is null.

  • I made some changes in the class, but still can’t find the error.

  • Are you using an IDE? In the porPontos.java class after Piloto p2 = (Piloto) obj2; place System.out.println("p1 = " + p1 + ", p2 = " + p2); that before it shows the error, it will have printed on the console that one of the people is null (it does not solve your problem that probably should be something with your logic, but it will show the data before happening Nullpointerexception).

  • I edited at the end of the code the error that appears when I enter this code.

  • Look what I told you: p1 = null, p2 = trabalhoga.Piloto@d325aef, P1 is null and you try the next if command (P1.getPts() == P2.getPts()) where P1 is null. You now need to figure out why P1 is null (bug in your code) or else to circumvent this message, put a if (p1 == null) { return 1; } (I do not recommend this option).

  • I was instantiating 30 pilots and only using 18, so I was null. I got this number and it worked fine. Tks!

Show 1 more comment

1 answer

1

porPontos.java:21

A variable referenced in the class porPontos.java on line 21 is null. Just check or change the logic to make sure it doesn’t get here with null value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.