title: Treat NullPointerException appropriately
body: <p>I've got a PurchaseOrderList class with a following method, which returns an Array of objects in the list.</p>
  
  <pre><code>public PurchaseOrder [] getAllOrders()
  {   
      PurchaseOrder [] ordersArray = new PurchaseOrder[listOfOrders.size()];
  
      if (listOfOrders.isEmpty())
          ordersArray = null;
      else
          for(int i = 0; i &lt; listOfOrders.size(); i++)
          {
              ordersArray[i] = listOfOrders.get(i);
          }
  
      return ordersArray;
  }
  </code></pre>
  
  <p>If it is empty, it MUST return null!</p>
  
  <p>I need to maintain 2 lists - one for the current and one for the last year. If it is a new year, the current year's data must be overwritten to lastYearList. And current year's list must be cleared. At the same time, discounts are based on totalOrdersValue the customer made during the relevant period, so I need to subtract the value of pasts orders for each customer from pastYearList, before it will get overwitten. </p>
  
  <p>I've got the following, but don't know how to optimise it..</p>
  
  <pre><code>public void startNewMonth()
  {
      PurchaseOrder [] lastYearArray = lastYearList.getAllOrders();
      PurchaseOrder [] currentYearArray = currentYearList.getAllOrders(); 
  
      try
      {
          if(lastYearList != null)
          {
              for(int i = 0; i &lt; lastYearArray.length; i++)
              {
                  String foundID = lastYearArray[i].getCustomersID();
                  int valueOfOrder = lastYearArray[i].getFullPriceValue();
  
                  customersList.findCustomer(foundID).
                                        updateTotalOrdersValue(-valueOfOrder);
              }
          }
  
          System.arraycopy(currentYearArray, 0, lastYearArray, 0,
                                                     currentYearArray.length);            
  
          for(int i = 0; i &lt; lastYearArray.length; i++)
          {
              lastYearList.addPurchaseOrder(lastYearArray[i]);
          }
  
          currentYearList.setListToEmpty();
      }
  
      catch(Exception e)
      {
          System.out.println(e);
      }
  }
  </code></pre>
  
  <p>It seems that NullPointerException is thrown from the first line (if there is no data), but if I put it into 'try', the code gets very lengthy and I have to type a lot of the same stuff for different try as well as catch statements. I believe there is a short way to do that.</p>
  
  <p>Thanks very much!</p>