Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8967527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:15:49+00:00 2026-06-15T17:15:49+00:00

I can find plenty of Q&A on here about whether an arraylist can equal

  • 0

I can find plenty of Q&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:

    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);
    }
}
}

Here is the Item class:

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;
}

}

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):

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();
    }
}

Thanks in advance for all your help

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:

    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);
}
}

and my test methods look like this:

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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:15:50+00:00Added an answer on June 15, 2026 at 5:15 pm

    Your add routine is a little off. It tries to use an item that is already in the list rather than creating a new one. Try this:

    void addInvoiceItem(Item item, Integer quantity, double discount) {
        if(
            item == null || 
            quantity == null || 
            item.sku == null ||
            item.description == null
        ) {
            throw new NullPointerException();
        }
        InvoiceItem invoice = new InvoiceItem(item, quantity, discount);
        invoiceList.add(invoice);
    }
    

    Also take a look a checkNotNull( ) from the google library to reduce typing a bit.

    It may be useful to check for null in the InvoiceItem constructor rather than in the adder, unless you want to allow nulls elsewhere.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can find plenty here about quotes in PSQL but nothing that quite fits
I am looking for the red dots showing here I can find find plenty
I can find plenty of questions about making objects support multiple protocols but none
I can find plenty of info on how msi upgrades. E.g. info about minor
Where can find resources about best practices for SharePoint programming? I am talking about
I can find no non-deprecated way of hiding an item in a menu bar
I can find plenty of examples that show how to configure a URL to
I can find plenty of documentation as to issues with use of time() prior
There are plenty of question about join on the same table, but I can't
i've done plenty of googling and whatnot and can't find quite what i'm looking

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.