title: Identify record that is culprit - coding practices
body: <p>Is method chaining good?</p>
  
  <p>I am not against functional programming that uses method chaining a lot, but against a herd mentality where people mindlessly run behind something that is new.</p>
  
  <p>The example, if I am processing a list of items using stream programming and need to find out the exact row that resulted into throwing <code>NullPointerException</code>.</p>
  
  <pre><code>private void test() {
      List&lt;User&gt; aList = new ArrayList&lt;&gt;();
      // fill aList with some data
      aList.stream().forEach(x -&gt; doSomethingMeaningFul(x.getAddress()));
  }
  
  private void doSomethingMeaningFul(Address x)   {
       // Do something
  }
  </code></pre>
  
  <p>So in the example above if any object in list is null, it will lead to <code>NullPointerException</code> while calling <code>x.getAddress()</code> and come out, without giving us a hook to identify a User record which has this problem.</p>
  
  <p>I may be missing something that offers this feature in stream programming, any help is appreciated.</p>
  
  <p>Edit 1:
  NPE is just an example, but there are several other RuntimeExceptions that could occur.  Writing filter would essentially mean checking for every RTE condition based on the operation I am performing.  And checking for every operation will become a pain.</p>
  
  <p>To give a better idea about what I mean following is the snippet using older methods; I couldn't find any equivalent with streams / functional programming methods.</p>
  
  <pre><code>List&lt;User&gt; aList = new ArrayList&lt;&gt;();
  // Fill list with some data
  int counter = 0;
  User u = null;
  
  try {
        for (;counter &lt; aList.size(); counter++) {
            u = aList.get(counter);
            u.doSomething();
            int result = u.getX() / u.getY();
        }
  } catch(Exception e)  {
    System.out.println("Error processing at index:" + counter + " with User record:" + u);
    System.out.println("Exception:" + e);
  }
  
  This will be a boon during the maintenance phase(longest phase) pointing exact data related issues which are difficult to reproduce.
  **Benefits:**
  - Find exact index causing issue, pointing to data
  - Any RTE is recorded and analyzed against the user record
  - Smaller stacktrace to look at
  </code></pre>