2017-10-10 2 views
2

J'ai le programme ci-dessous dans lequel j'essaie d'imprimer null. J'utilise Java 8 pour cela. Ci-dessous mon code pour y parvenir:Obtenir l'exception ci-dessous sur l'utilisation de la première méthode null de java 8

  List<Person> personList = new ArrayList<>();         
      personList.add(new Person("Alice", "[email protected]", Gender.FEMALE, 15)); 
      personList.add(new Person("Bob", "[email protected]", Gender.MALE, 16));   
      personList.add(new Person("Eric", "[email protected]", Gender.MALE, 17)); 
      personList.add(new Person("Carol", "[email protected]", Gender.FEMALE, 23)); 
      personList.add(new Person(null, "[email protected]", Gender.FEMALE, 15)); 
      personList.add(new Person("Carol", "[email protected]", Gender.FEMALE, 23)); 
      personList.add(new Person("David", "[email protected]", Gender.MALE, 19)); 
      personList.add(new Person("Bob", "[email protected]", Gender.MALE, 16)); 

maintenant ci-dessous est le code que je l'ai écrit pour imprimer le nom null premier:

personList.stream().sorted(Comparator.nullsFirst(Comparator.comparing(Person::getName))).forEach(System.out::println); 

mais je reçois l'exception ci-dessous, s'il vous plaît indiquer comment surmonter de cela?

Exception in thread "main" java.lang.NullPointerException 
    at java.util.Comparator.lambda$comparing$77a9974f$1(Comparator.java:469) 
    at java.util.Comparators$NullComparator.compare(Comparators.java:83) 
    at java.util.TimSort.binarySort(TimSort.java:296) 
    at java.util.TimSort.sort(TimSort.java:221) 
+2

'forEach (System.out :: println)' est la raison probable là. – nullpointer

+0

lire https://stackoverflow.com/questions/26350996/java-8-comparator-nullsfirst-naturalorder-confused –

Répondre

1

Le NPE est un résultat du passage d'une valeur null à Comparator.comparing

Le Javadoc dit

@throws NullPointerException si l'argument est nul

Peut-être essayer quelque chose comme

Comparator.comparing(Person::getName, Comparator.nullsFirst(String.CASE_INSENSITIVE_ORDER)) 

qui produit

null 
Alice 
Bob 
Bob 
Carol 
Carol 
David 
Eric 
2

C'est parce que vous utilisez mauvais comparateur. Voir ceci et doit fonctionner

import java.util.stream.*; 
import static java.util.Comparator.*; 
import java.util.*; 
public class HelloWorld{ 

    public static void main(String []args){ 

     List<Person> personList = new ArrayList<>();         
      personList.add(new Person("Alice", "[email protected]")); 
      personList.add(new Person("Bob", "[email protected]"));   
      personList.add(new Person("Eric", "[email protected]")); 
      personList.add(new Person("Carol", "[email protected]")); 
      personList.add(new Person(null, "[email protected]")); 
      personList.add(new Person("Carol", "[email protected]")); 
      personList.add(new Person("David", "[email protected]")); 
      personList.add(new Person("Bob", "[email protected]")); 
      System.out.println(personList); 



      personList.stream().sorted(comparing(Person::getName, nullsFirst(naturalOrder()))).forEach(System.out::println); 
    } 
} 
class Person 
{ 
    private String name,email; 
    Person(String name, String email) 
    { 
     this.name=name; 
    } 
    public String getName() 
    { 
     return name; 
    } 
    public String toString(){return name;} 
} 
+0

Peut aussi jeter un oeil à cet article: https://stackoverflow.com/questions/26350996/java- 8-comparateur-nullsfirst-naturalorder-confused – Dustin

+0

thx @Dustin. J'allais ajouter plus d'explications. mais votre fil semble déjà avoir tout. – Optional