-1

Here is my example:

import java.util.ArrayList;
import java.util.stream.IntStream;


public class Example {

     public static void main( String[] args )  {

         ArrayList<Integer> list = new ArrayList<>();
         list.add(0);list.add(1);list.add(2);

         SomeObject  a = IntStream.range(0,list.size())
                  .filter(i->list.get(i)==3 || list.get(i)==4)
                  .mapToObj(i->new SomeObject(i,list.get(i)))
                  .findFirst()
                  .orElse(null);
         System.out.println(a);
   }
}

class SomeObject{
    int index;
    int value;
    SomeObject(int index,int value){
            this.index=index;
            this.value=value;
    }

    @Override
    public String toString() {
        return this.index+" "+this.value;
    }
}

Why when we are calling System.out.println(a); we don't get a NullPointerException?

As the orElse(null) returns an object pointing to null. If not what is the difference between the null in the orElse method and an object pointing to null?

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
xmen-5
  • 1,806
  • 1
  • 23
  • 44
  • 3
    `System.out.println` can print a `null` reference. – Eran Nov 22 '18 at 11:49
  • why it is not the case for an optional pointing to null. for that i was confused.if i put .get(); in place of orElse(null), i will get an exception. – xmen-5 Nov 22 '18 at 11:53
  • for that sake `SomeObject b =null; System.out.println(b);` would not throw an NPE either. Strongly believe that this is a duplicate. Voting to close as duplicate. There are answer stating, what causes NPE as well in the linked question. – Naman Nov 22 '18 at 13:15

2 Answers2

1

Let's go a little bit in detail...

System.out.println(a); calls this overload of println:

public void println(Object x)

which states:

Prints an Object and then terminate the line. This method calls at first String.valueOf(x) to get the printed object's string value, then behaves as though it invokes print(String) and then println().

Given the above let's go to the String.valueOf method:

public static String valueOf(Object obj)

which states:

Returns: if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

Since obj is null a "null" is returned and obj.toString() is never invoked hence no exception.


As for your following comment:

why it is not the case for an optional pointing to null. for that i was confused.if i put .get(); in place of orElse(null), i will get an exception.

an Optional<T> doesn't "point to null" (wrong terminology being used, usually we say an Optional either has a present state or absent state).

Invoking .get() carelessly from an Optional<T> should be avoided unless absolutely sure the Optional<T> is not empty.

You receive a NoSuchElementException in this case simply because there is no value present and this is documented in the Optional<T> API.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
1

why it is not the case for an optional pointing to null.

To answer this comment as I think this is what you are trying to ask in the question, see this answer.

An optional, introduced in Java 8, is a container object which is used to contain specifically, not-null objects. Instead, optional object is used to represent null with absent value.

The fact that the findFirst() method returns Optional is a questionable design decision. But that's just how it is currently.

John Kim
  • 1,081
  • 10
  • 26