0

My problem is that a collection has a null value, and any "utils" method return that the collection is not empty. There are other "elegant" options?

This code throws a null pointer exception:

    public static void main(String[] args) {
    ArrayList<String> strings = new ArrayList<>();
    strings.add(null);

    if(CollectionUtils.isNotEmpty(strings)) {
        for (String s : strings) {
            System.out.println(s.length());
        }
    }
}
  • Welcome to Stack Overflow! Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, _a specific problem or error_ and _the shortest code necessary_ to reproduce it **in the question itself**. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Joe C Jul 06 '17 at 22:22
  • 4
    A collection containing null _isn't_ empty. If your collection can contain null, you'll need to check for that when iterating over it. (But 99% of the time you're better off just making it impossible for your collection to contain null.) – Louis Wasserman Jul 06 '17 at 22:24
  • This isn't a duplicate of https://stackoverflow.com/questions/34305512/what-is-difference-between-null-and-empty-list - the OP here is asking "is there a util method for detecting whether a list contains a null?". – Oliver Charlesworth Jul 06 '17 at 22:25
  • Welcome to Java. Really though, you're probably just best off just doing a null check in the loop before trying to access it. – kunruh Jul 06 '17 at 22:25
  • @OliverCharlesworth I marked that by accident (I was about to mark that one, and still had it in my copy buffer; the next copy didn't work), and had already changed it to this: https://stackoverflow.com/questions/33902195/difference-between-an-empty-arraylist-and-an-arraylist-with-null-elements – Andy Turner Jul 06 '17 at 22:26
  • Also: in the example, the `if(CollectionUtils.isNotEmpty(strings))` is essentially a no-op. If you drop it, the result is exactly the same. Either don't ever add `null` or filter the nulls during traveral. Java 8 [makes this beautifully easy](https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#filter-java.util.function.Predicate-). – dhke Jul 06 '17 at 22:29
  • Even if you were able to detect that the collection contains a `null` somewhere, you'd still need to check every value, because you wouldn't know which value is `null` and which value isn't. So either remove the `null`s or skip them. – fps Jul 07 '17 at 03:29

1 Answers1

1

You can check if there are nulls in the collection and/or filter like in the following code:

public static void main(String[] args) {
    List<String> strings = new ArrayList<>();
    strings.add("one");
    strings.add(null);
    strings.add("two");

    // has a null value?
    final boolean hasNulls = strings.stream()
            .anyMatch(Objects::isNull);
    System.out.println("has nulls: " + hasNulls);

    // filter null values
    strings = strings.stream()
            .filter(Objects::nonNull)
            .collect(Collectors.toList());

    System.out.println("filtered: " + strings.toString());
}
P.J.Meisch
  • 18,013
  • 6
  • 50
  • 66