title: Check for null field in arraylist
body: <p>I can find plenty of Q&amp;A on here about whether an arraylist can equal null, which was helpful in its own way, but I can't find an answer for throwing errors if any fields in the arraylist are null. As I'm adding objects to the arraylist, I want to throw an exception if the user tries to pass in anything that is null. Here is the code:</p>
  
  <pre><code>    void addInvoiceItem(Item item, Integer quantity, double discount) throws Exception {    
  for (InvoiceItem thing: invoiceList) {
      if (thing == null) {
          throw new IllegalArgumentException("Part of the invoice item is blank (null). Please review your invoice items and ensure you have specified values for the item.");
      }
      else {
          invoiceList.add(thing);
          thing.setItemQuantity(quantity);
          thing.setItemDiscount(discount);
          System.out.println(invoiceList);
      }
  }
  }
  </code></pre>
  
  <p>Here is the Item class:</p>
  
  <pre><code>final class Item {
  
  String itemDescription;
  double itemPrice;
  Integer itemSKU;
  
  Item (String description, double price, Integer sku) {
      this.itemDescription = description;
      this.itemPrice = price;
      this.itemSKU = sku;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here are the test methods that are letting me know I'm definitely omitting something. One is to test for a valid InvoiceItem, the other for an invalid one (contains nulls):</p>
  
  <pre><code>public class InvoiceTest {
  //create the static values to be used
  //for InvoiceItem
  String goodDescription = "wheel";
  double goodPrice = 500.00;
  Integer goodSku = 0002;
  Item goodInvoiceItem = new Item(goodDescription, goodPrice, goodSku);
  
  String emptyDescription = null;
  double emptyPrice = 0;
  Integer emptySku = 0;
  Item badInvoiceItem = new Item(emptyDescription, emptyPrice, emptySku);
  
  Integer itemQuantity = 0;
  double itemDiscount = 0.05;
  
  @Test
  public void invalidItemAddTest() {
      Invoice badInvoice = new Invoice(null);
      try {
          badInvoice.addInvoiceItem(badInvoiceItem, itemQuantity, itemDiscount);
          System.out.println(badInvoice);
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  
  @Test
  public void validItemAddTest() {
      Invoice goodInvoice = new Invoice(null);
      try {
          goodInvoice.addInvoiceItem(goodInvoiceItem, itemQuantity, itemDiscount);
          System.out.println(goodInvoice);
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>Thanks in advance for all your help</p>
  
  <p>Edit with additions:
  So Mel's answer was my starting point and I made some additions to get it working the way I needed to. My adding method now looks like this:</p>
  
  <pre><code>    void addInvoiceItem(Item item, Integer quantity, double discount) {
      if(item == null || quantity == 0 || discount == 0) {
          throw new IllegalArgumentException("Part of the invoice item is blank (null). Please review your invoice items and ensure you have specified values for the item.");
      } else {
      InvoiceItem invoice = new InvoiceItem(item, quantity, discount);
      invoiceList.add(invoice);
  }
  }
  </code></pre>
  
  <p>and my test methods look like this:</p>
  
  <pre><code>public class InvoiceTest {
  //create the static values to be used
  //for InvoiceItem
  String goodDescription = "wheel";
  double goodPrice = 500.00;
  int goodSku = 0002;
  Item goodInvoiceItem = new Item(goodDescription, goodPrice, goodSku);
  
  String emptyDescription = null;
  double emptyPrice = 0;
  int emptySku = 0;
  Item badInvoiceItem = new Item(emptyDescription, emptyPrice, emptySku);
  
  int badItemQuantity = 0;
  double badItemDiscount = 0;
  int goodItemQuantity = 1;
  double goodItemDiscount = 0.05;
  
  
  /**
   * @Before - initialize what we need for the test
   * @throws Exception
   */
  @Before
  public void setUp() {
      //things needed for testInvalidItemAdd()
  
  
  }
  
  /**
   * @throws Exception 
   * @Test - confirm you cannot add an item that is null
   */
  @Test
  public void invalidItemAddTest() {
      boolean exceptionThrown = false;
      Invoice badInvoice = new Invoice(null);
      try {
          badInvoice.addInvoiceItem(badInvoiceItem, badItemQuantity, badItemDiscount);
          System.out.println(badInvoice);
      } catch (Exception e) {
          e.printStackTrace();
          exceptionThrown = true;
      }
      assertTrue(exceptionThrown);
  }
  
  @Test
  public void validItemAddTest() {
      boolean exceptionThrown = false;
      Invoice goodInvoice = new Invoice(null);
      try {
          goodInvoice.addInvoiceItem(goodInvoiceItem, goodItemQuantity, goodItemDiscount);
          System.out.println(goodInvoice);
      } catch (Exception e) {
          e.printStackTrace();
          exceptionThrown = true;
      }
      assertFalse(exceptionThrown);
  }
  </code></pre>